2
0

Canvas.st 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. !HTMLCanvas class methodsFor: 'instance creation'!
  167. onJQuery: aJQuery
  168. ^self basicNew
  169. initializeFromJQuery: aJQuery;
  170. initialize;
  171. yourself
  172. ! !
  173. Object subclass: #TagBrush
  174. instanceVariableNames: 'canvas element'
  175. category: 'Canvas'!
  176. !TagBrush methodsFor: 'accessing'!
  177. element
  178. ^element
  179. ! !
  180. !TagBrush methodsFor: 'adding'!
  181. contents: anObject
  182. self asJQuery empty.
  183. self append: anObject
  184. !
  185. addBrush: aTagBrush
  186. self appendChild: aTagBrush element.
  187. ^aTagBrush
  188. !
  189. with: anObject
  190. self append: anObject
  191. !
  192. append: anObject
  193. anObject appendToBrush: self
  194. !
  195. appendToBrush: aTagBrush
  196. aTagBrush addBrush: self
  197. !
  198. appendBlock: aBlock
  199. | root |
  200. root := canvas root.
  201. canvas root: self.
  202. aBlock value: canvas.
  203. canvas root: root
  204. !
  205. appendChild: anElement
  206. <self['@element'].appendChild(anElement)>
  207. !
  208. appendString: aString
  209. self appendChild: (self createTextNodeFor: aString)
  210. ! !
  211. !TagBrush methodsFor: 'attributes'!
  212. at: aString put: aValue
  213. <self['@element'].setAttribute(aString, aValue)>
  214. !
  215. removeAt: aString
  216. <self['@element'].removeAttribute(aString)>
  217. !
  218. class: aString
  219. self at: 'class' put: aString
  220. !
  221. id: aString
  222. self at: 'id' put: aString
  223. !
  224. src: aString
  225. self at: 'src' put: aString
  226. !
  227. href: aString
  228. self at: 'href' put: aString
  229. !
  230. title: aString
  231. self at: 'title' put: aString
  232. !
  233. style: aString
  234. self at: 'style' put: aString
  235. ! !
  236. !TagBrush methodsFor: 'converting'!
  237. asJQuery
  238. <return smalltalk.JQuery._from_(jQuery(self['@element']))>
  239. !
  240. asJQueryDo: aBlock
  241. aBlock value: self asJQuery
  242. ! !
  243. !TagBrush methodsFor: 'events'!
  244. onKeyDown: aBlock
  245. self asJQuery on: 'keydown' do: aBlock
  246. !
  247. onKeyPress: aBlock
  248. self asJQuery on: 'keypress' do: aBlock
  249. !
  250. onKeyUp: aBlock
  251. self asJQuery on: 'keyup' do: aBlock
  252. !
  253. onFocus: aBlock
  254. self asJQuery on: 'focus' do: aBlock
  255. !
  256. onBlur: aBlock
  257. self asJQuery on: 'blur' do: aBlock
  258. !
  259. onChange: aBlock
  260. self asJQuery on: 'change' do: aBlock
  261. !
  262. onClick: aBlock
  263. self asJQuery on: 'click' do: aBlock
  264. ! !
  265. !TagBrush methodsFor: 'initialization'!
  266. initializeFromString: aString canvas: aCanvas
  267. element := self createElementFor: aString.
  268. canvas := aCanvas
  269. !
  270. initializeFromJQuery: aJQuery canvas: aCanvas
  271. element := aJQuery jquery get: 0.
  272. canvas := aCanvas
  273. ! !
  274. !TagBrush methodsFor: 'private'!
  275. createElementFor: aString
  276. <return document.createElement(String(aString))>
  277. !
  278. createTextNodeFor: aString
  279. <return document.createTextNode(String(aString))>
  280. ! !
  281. !TagBrush class methodsFor: 'instance creation'!
  282. fromString: aString canvas: aCanvas
  283. ^self new
  284. initializeFromString: aString canvas: aCanvas;
  285. yourself
  286. !
  287. fromJQuery: aJQuery canvas: aCanvas
  288. ^self new
  289. initializeFromJQuery: aJQuery canvas: aCanvas;
  290. yourself
  291. ! !
  292. Object subclass: #Widget
  293. instanceVariableNames: ''
  294. category: 'Canvas'!
  295. !Widget methodsFor: 'actions'!
  296. alert: aString
  297. <alert(aString)>
  298. !
  299. confirm: aString
  300. <return window.confirm(aString)>
  301. !
  302. prompt: aString
  303. ^self prompt: aString default: ''
  304. !
  305. prompt: aString default: anotherString
  306. <return window.prompt(aString, anotherString)>
  307. ! !
  308. !Widget methodsFor: 'adding'!
  309. appendToBrush: aTagBrush
  310. self appendToJQuery: aTagBrush asJQuery
  311. !
  312. appendToJQuery: aJQuery
  313. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  314. ! !
  315. !Widget methodsFor: 'rendering'!
  316. renderOn: html
  317. self
  318. ! !
  319. TagBrush subclass: #CanvasBrush
  320. instanceVariableNames: ''
  321. category: 'Canvas'!
  322. !CanvasBrush methodsFor: 'initialization'!
  323. initializeWithCanvas: aCanvas
  324. canvas := aCanvas
  325. ! !
  326. !CanvasBrush methodsFor: 'private'!
  327. createElement
  328. <return document.createElement('canvas')>
  329. ! !
  330. !CanvasBrush class methodsFor: 'instance creation'!
  331. canvas: aCanvas
  332. ^self new
  333. initializeWithCanvas: aCanvas;
  334. yourself
  335. ! !
  336. !Object methodsFor: '*Canvas'!
  337. appendToBrush: aTagBrush
  338. aTagBrush append: self asString
  339. ! !
  340. !BlockClosure methodsFor: '*Canvas'!
  341. appendToBrush: aTagBrush
  342. aTagBrush appendBlock: self
  343. ! !
  344. !String methodsFor: '*Canvas'!
  345. appendToBrush: aTagBrush
  346. aTagBrush appendString: self
  347. ! !