Canvas.st 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. Object subclass: #CanvasRenderingContext
  2. instanceVariableNames: ''
  3. category: 'Canvas'!
  4. !CanvasRenderingContext methodsFor: 'drawing arcs'!
  5. arcTo: aPoint radius: aNumber startAngle: aNumber2 endAngle: aNumber3 anticlockwise: aBoolean
  6. <self.arc(aPoint._x(), aPoint._y(), aNumber, aNumber2, aNumber3, aBoolean)>
  7. !
  8. arcTo: aPoint radius: aNumber
  9. self arcTo: aPoint radius: aNumber startAngle: 0 endAngle: Number pi * 2 anticlockwise: false
  10. ! !
  11. !CanvasRenderingContext methodsFor: 'drawing paths'!
  12. fillStyle: aString
  13. <self.fillStyle = String(aString)>
  14. !
  15. beginPath
  16. <self.beginPath()>
  17. !
  18. closePath
  19. <self.closePath()>
  20. !
  21. fill
  22. <self.fill()>
  23. !
  24. stroke
  25. <self.stroke()>
  26. !
  27. moveTo: aPoint
  28. <self.moveTo(aPoint._x(), aPoint._y())>
  29. !
  30. lineTo: aPoint
  31. <self.lineTo(aPoint._x(), aPoint._y())>
  32. !
  33. strokeStyle: aString
  34. <self.strokeStyle = String(aString)>
  35. !
  36. lineWidth: aNumber
  37. <self.lineWidth = aNumber>
  38. ! !
  39. !CanvasRenderingContext methodsFor: 'drawing rectangles'!
  40. fillRectFrom: aPoint to: anotherPoint
  41. <self.fillRect(aPoint._x(), aPoint._y(), anotherPoint._x(), anotherPoint._y())>
  42. !
  43. strokeRectFrom: aPoint to: anotherPoint
  44. <self.strokeRect(aPoint._x(), aPoint._y(), anotherPoint._x(), anotherPoint._y())>
  45. !
  46. clearRectFrom: aPoint to: anotherPoint
  47. <self.fillRect(aPoint._x(), aPoint._y(), anotherPoint._x(), anotherPoint._y())>
  48. ! !
  49. !CanvasRenderingContext class methodsFor: 'instance creation'!
  50. tagBrush: aTagBrush
  51. <return aTagBrush._element().getContext('2d')>
  52. ! !
  53. Object subclass: #HTMLCanvas
  54. instanceVariableNames: 'root'
  55. category: 'Canvas'!
  56. !HTMLCanvas methodsFor: 'accessing'!
  57. root: aTagBrush
  58. root := aTagBrush
  59. !
  60. root
  61. ^root
  62. ! !
  63. !HTMLCanvas methodsFor: 'adding'!
  64. with: anObject
  65. ^self root with: anObject
  66. ! !
  67. !HTMLCanvas methodsFor: 'initialization'!
  68. initialize
  69. super initialize.
  70. root ifNil: [root := TagBrush fromString: 'div' canvas: self]
  71. !
  72. initializeFromJQuery: aJQuery
  73. root := TagBrush fromJQuery: aJQuery canvas: self
  74. ! !
  75. !HTMLCanvas methodsFor: 'tags'!
  76. newTag: aString
  77. ^TagBrush fromString: aString canvas: self
  78. !
  79. tag: aString
  80. ^root addBrush: (self newTag: aString)
  81. !
  82. h1
  83. ^self tag: 'h1'
  84. !
  85. h2
  86. ^self tag: 'h2'
  87. !
  88. h3
  89. ^self tag: 'h3'
  90. !
  91. h4
  92. ^self tag: 'h4'
  93. !
  94. h5
  95. ^self tag: 'h5'
  96. !
  97. h6
  98. ^self tag: 'h6'
  99. !
  100. p
  101. ^self tag: 'p'
  102. !
  103. div
  104. ^self tag: 'div'
  105. !
  106. span
  107. ^self tag: 'span'
  108. !
  109. img
  110. ^self tag: 'img'
  111. !
  112. ul
  113. ^self tag: 'ul'
  114. !
  115. ol
  116. ^self tag: 'ol'
  117. !
  118. li
  119. ^self tag: 'li'
  120. !
  121. table
  122. ^self tag: 'table'
  123. !
  124. tr
  125. ^self tag: 'tr'
  126. !
  127. td
  128. ^self tag: 'td'
  129. !
  130. th
  131. ^self tag: 'th'
  132. !
  133. form
  134. ^self tag: 'form'
  135. !
  136. input
  137. ^self tag: 'input'
  138. !
  139. button
  140. ^self tag: 'button'
  141. !
  142. select
  143. ^self tag: 'select'
  144. !
  145. option
  146. ^self tag: 'option'
  147. !
  148. textarea
  149. ^self tag: 'textarea'
  150. !
  151. a
  152. ^self tag: 'a'
  153. !
  154. canvas
  155. ^self tag: 'canvas'
  156. !
  157. pre
  158. ^self tag: 'pre'
  159. !
  160. code
  161. ^self tag: 'code'
  162. !
  163. br
  164. ^self tag: 'br'
  165. !
  166. script
  167. ^self tag: 'script'
  168. !
  169. link
  170. ^self tag: 'link'
  171. ! !
  172. !HTMLCanvas class methodsFor: 'instance creation'!
  173. onJQuery: aJQuery
  174. ^self basicNew
  175. initializeFromJQuery: aJQuery;
  176. initialize;
  177. yourself
  178. ! !
  179. Object subclass: #TagBrush
  180. instanceVariableNames: 'canvas element'
  181. category: 'Canvas'!
  182. !TagBrush methodsFor: 'accessing'!
  183. element
  184. ^element
  185. ! !
  186. !TagBrush methodsFor: 'adding'!
  187. contents: anObject
  188. self asJQuery empty.
  189. self append: anObject
  190. !
  191. addBrush: aTagBrush
  192. self appendChild: aTagBrush element.
  193. ^aTagBrush
  194. !
  195. with: anObject
  196. self append: anObject
  197. !
  198. append: anObject
  199. anObject appendToBrush: self
  200. !
  201. appendToBrush: aTagBrush
  202. aTagBrush addBrush: self
  203. !
  204. appendBlock: aBlock
  205. | root |
  206. root := canvas root.
  207. canvas root: self.
  208. aBlock value: canvas.
  209. canvas root: root
  210. !
  211. appendChild: anElement
  212. <self['@element'].appendChild(anElement)>
  213. !
  214. appendString: aString
  215. self appendChild: (self createTextNodeFor: aString)
  216. ! !
  217. !TagBrush methodsFor: 'attributes'!
  218. at: aString put: aValue
  219. <self['@element'].setAttribute(aString, aValue)>
  220. !
  221. removeAt: aString
  222. <self['@element'].removeAttribute(aString)>
  223. !
  224. class: aString
  225. self at: 'class' put: aString
  226. !
  227. id: aString
  228. self at: 'id' put: aString
  229. !
  230. src: aString
  231. self at: 'src' put: aString
  232. !
  233. href: aString
  234. self at: 'href' put: aString
  235. !
  236. title: aString
  237. self at: 'title' put: aString
  238. !
  239. style: aString
  240. self at: 'style' put: aString
  241. !
  242. type: aString
  243. self at: 'type' put: aString
  244. !
  245. media: aString
  246. self at: 'media' put: aString
  247. !
  248. rel: aString
  249. self at: 'rel' put: aString
  250. ! !
  251. !TagBrush methodsFor: 'converting'!
  252. asJQuery
  253. <return smalltalk.JQuery._from_(jQuery(self['@element']))>
  254. !
  255. asJQueryDo: aBlock
  256. aBlock value: self asJQuery
  257. ! !
  258. !TagBrush methodsFor: 'events'!
  259. onKeyDown: aBlock
  260. self asJQuery on: 'keydown' do: aBlock
  261. !
  262. onKeyPress: aBlock
  263. self asJQuery on: 'keypress' do: aBlock
  264. !
  265. onKeyUp: aBlock
  266. self asJQuery on: 'keyup' do: aBlock
  267. !
  268. onFocus: aBlock
  269. self asJQuery on: 'focus' do: aBlock
  270. !
  271. onBlur: aBlock
  272. self asJQuery on: 'blur' do: aBlock
  273. !
  274. onChange: aBlock
  275. self asJQuery on: 'change' do: aBlock
  276. !
  277. onClick: aBlock
  278. self asJQuery on: 'click' do: aBlock
  279. ! !
  280. !TagBrush methodsFor: 'initialization'!
  281. initializeFromString: aString canvas: aCanvas
  282. element := self createElementFor: aString.
  283. canvas := aCanvas
  284. !
  285. initializeFromJQuery: aJQuery canvas: aCanvas
  286. element := aJQuery jquery get: 0.
  287. canvas := aCanvas
  288. ! !
  289. !TagBrush methodsFor: 'private'!
  290. createElementFor: aString
  291. <return document.createElement(String(aString))>
  292. !
  293. createTextNodeFor: aString
  294. <return document.createTextNode(String(aString))>
  295. ! !
  296. !TagBrush class methodsFor: 'instance creation'!
  297. fromString: aString canvas: aCanvas
  298. ^self new
  299. initializeFromString: aString canvas: aCanvas;
  300. yourself
  301. !
  302. fromJQuery: aJQuery canvas: aCanvas
  303. ^self new
  304. initializeFromJQuery: aJQuery canvas: aCanvas;
  305. yourself
  306. ! !
  307. Object subclass: #Widget
  308. instanceVariableNames: ''
  309. category: 'Canvas'!
  310. !Widget methodsFor: 'actions'!
  311. alert: aString
  312. <alert(aString)>
  313. !
  314. confirm: aString
  315. <return window.confirm(aString)>
  316. !
  317. prompt: aString
  318. ^self prompt: aString default: ''
  319. !
  320. prompt: aString default: anotherString
  321. <return window.prompt(aString, anotherString)>
  322. ! !
  323. !Widget methodsFor: 'adding'!
  324. appendToBrush: aTagBrush
  325. self appendToJQuery: aTagBrush asJQuery
  326. !
  327. appendToJQuery: aJQuery
  328. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  329. ! !
  330. !Widget methodsFor: 'rendering'!
  331. renderOn: html
  332. self
  333. ! !
  334. TagBrush subclass: #CanvasBrush
  335. instanceVariableNames: ''
  336. category: 'Canvas'!
  337. !CanvasBrush methodsFor: 'initialization'!
  338. initializeWithCanvas: aCanvas
  339. canvas := aCanvas
  340. ! !
  341. !CanvasBrush methodsFor: 'private'!
  342. createElement
  343. <return document.createElement('canvas')>
  344. ! !
  345. !CanvasBrush class methodsFor: 'instance creation'!
  346. canvas: aCanvas
  347. ^self new
  348. initializeWithCanvas: aCanvas;
  349. yourself
  350. ! !
  351. !Object methodsFor: '*Canvas'!
  352. appendToBrush: aTagBrush
  353. aTagBrush append: self asString
  354. ! !
  355. !BlockClosure methodsFor: '*Canvas'!
  356. appendToBrush: aTagBrush
  357. aTagBrush appendBlock: self
  358. ! !
  359. !String methodsFor: '*Canvas'!
  360. appendToBrush: aTagBrush
  361. aTagBrush appendString: self
  362. ! !