Advertisement
Guest User

Untitled

a guest
Jul 26th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.48 KB | None | 0 0
  1. (allowed ()
  2. "!work" "!canvasTest" "@lib.css" )
  3. (allow "!setCanvasSize")
  4.  
  5. (load
  6. "@lib/http.l" "@lib/xhtml.l" "@lib/form.l"
  7. "@lib/canvas.l" "@lib/svg.l" )
  8.  
  9. (allow "!mk.svg")
  10.  
  11. ################################################################################
  12. # S V G H E L P E R M E T H O D S
  13. ################################################################################
  14.  
  15.  
  16. (de rndrect (X Y DX DY R Fill Stroke)
  17. (msg (glue " " (list "rndrect " X Y DX DY R Fill Stroke)))
  18. (prin
  19. "<rect x=\"" X
  20. "\" y=\"" Y
  21. "\" width=\"" DX
  22. "\" height=\"" DY
  23. "\" rx=\"" R
  24. "\" ry=\"" R
  25. "\" fill=\"" (or Fill "none")
  26. "\" stroke=\"" (or Stroke (if Fill "none" "black"))
  27. "\" stroke-width=\"" *StrokeWidth "\"" )
  28. (and *Style (htStyle @))
  29. (prinl "/>") )
  30.  
  31. (de <mysvg> (W H Z *DX *DY P . "Prg")
  32. (msg (glue " " (list "mksvg " W H Z *DX *DY P . "Prg")))
  33. (prin
  34. "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink"
  35. "\" width=\"" (if (num? Z) (* @ W) (pack W Z))
  36. "\" height=\"" (if (num? Z) (* @ H) (pack H Z))
  37. "\" viewBox=\"0 0 " *DX " " *DY "\""
  38. (if P "" " preserveAspectRatio=\"none\""))
  39. (dfltCss "svg")
  40. (prinl ">")
  41. (let *Pos 0
  42. (prog1
  43. (run "Prg")
  44. (prinl "</svg>") ) ) )
  45.  
  46.  
  47. (de gridline (Stroke . @)
  48. (prin "<polyline fill=\"none\" stroke=\"" Stroke "\" stroke-width=\"" *StrokeWidth "\" stroke-dasharray=\"1 9\" points=\"" (next))
  49. (while (args)
  50. (prin " " (next)) )
  51. (prin "\"")
  52. (and *Style (htStyle @))
  53. (prinl "/>") )
  54.  
  55. (de drawGrid ()
  56. (msg "draing grid")
  57. (let? G 100
  58. (for (X 0 (>= 1000 X) (+ X G))
  59. (for (Y 0 (>= 1000 Y) (+ Y G))
  60. (when (=0 X) ( gridline "blue" X Y 1000 Y) ) # horiz lines
  61. (when (=0 Y) ( gridline "red" X 0 X 100) ( gridline "black" X 200 X 1000) ) # vertical lines
  62. ) ) ) )
  63.  
  64.  
  65.  
  66. (de makeSvg ()
  67. (<mysvg> 100 100 "%" 1000 1000 NIL # suitable for 10 x 10 grid with each
  68. (drawGrid)
  69. # (window 0 0 100 100 (rndrect 0 0 100 100 15 NIL "red")
  70. # (down 20) (font ( 30 . "Courier") (ps 0 "A" '(s "1"))))
  71. (drawTiles)
  72. ) )
  73.  
  74. (de mk.svg (Page)
  75. (msg "mk.svg ")
  76. (httpHead "image/svg+xml" 0)
  77. (ht:Out *Chunked
  78. (makeSvg) ) )
  79.  
  80.  
  81.  
  82. ################################################################################
  83. # T I L E H E L P E R S
  84. ################################################################################
  85.  
  86.  
  87. (de tileValue (L)
  88. (cond
  89. ( (= L " ") 1)
  90. ( (member L '(a e i o u)) 1)
  91. ( (member L '(z q x)) 5)
  92. (T 2)
  93. ) )
  94.  
  95.  
  96. (de vowel (Let)
  97. (cond
  98. ( (member Let '(a e i o u " ")) T)
  99. (T NIL)
  100. ) )
  101.  
  102. (de numVowels (Lst)
  103. (let S 0
  104. (mapcar '((X) (when (vowel X) (inc 'S))) Lst)
  105. S)
  106. )
  107.  
  108. # rules for a list of letters to be good
  109. (de areLettersOk (Lst)
  110. (let V (numVowels Lst)
  111. (cond
  112. ((> V 5 ) NIL) # no more than 5 vowels
  113. ((< V 3 ) NIL) # at least 3 vowels needed
  114. (T T) # ok by deafult
  115. ) ) )
  116.  
  117.  
  118. ################################################################################
  119. # T I L E M E T H O D S
  120. ################################################################################
  121.  
  122.  
  123. (class +Tile)
  124. # x y : location of upper left corner in a grid 1000 x 1000
  125. # l : letter tile represents
  126. # v : value of the tile
  127. # f : the fill color of the tile
  128. # s : the color of the tile edges
  129. # sx, sy, r (tile width, height and radius copied from tile manager...constants)
  130.  
  131. (dm T (X Y L F S Sx Sy R)
  132. (=: x X)
  133. (=: y Y)
  134. (=: l L)
  135. (=: v (tileValue L))
  136. (=: f F)
  137. (=: s S)
  138. (=: sx Sx)
  139. (=: sy Sy)
  140. (=: r R)
  141. )
  142.  
  143. (dm move> (NX NY) (:= x NX) (:= y NY))
  144.  
  145. (dm name> () (if (= " " (: l)) "--" (uppc (: l))))
  146.  
  147. (dm draw> ()
  148. (window (: x) (: y) (: sx) (: sy) (rndrect 6 6 (- (: sx) 12) (- (: sy) 12) (: r) (: f) (: s))
  149. (down (/ sy 5)) (font ( 30 . "Courier") (ps 0 (name> This (: l)) (list 's (: v)))) )
  150. )
  151.  
  152.  
  153. ################################################################################
  154. # T I L E M G R M E T H O D S
  155. ################################################################################
  156.  
  157. (class +TileMgr)
  158.  
  159. # some static class members
  160. # LetList: list of letters in the alphabet
  161. # LetNum: amounts of letters in a pile of 180 letters
  162.  
  163. (var Letters . (a b c d e f g h i j k l m n o p q r s t u v w x y z " ")) # unmodifiable
  164. (var Counts . (16 3 3 6 22 3 4 4 16 2 4 6 4 9 14 3 2 9 8 9 8 3 3 2 3 2 12)) # unmodifiable
  165. (var TotTiles 0)
  166. (var LetterAmounts NIL) # created by init
  167. (var LetterList NIL) # list of 10 letters finally selected
  168. (var NumLetters . 10 )
  169. (var Sx . 0) # Sx : width and height of the tile
  170. (var Sy . 0) # Sy : width and height of the tile
  171. (var R . 0) # R : the radius of the rounded edges in the tile rectangle
  172. (var TileList NIL) # list of 10 tiles, one per letter in Letter List
  173.  
  174. (dm genLetters> ()
  175. (let Lst NIL
  176. (while (> 10 (length Lst))
  177. (setq Lst
  178. (make
  179. (do (get This 'NumLetters)
  180. (setq R (rand 1 (car (get This 'TotTiles)) ))
  181. # find the character whose cum count corresponds to random var
  182. (setq X (car (find '((X) (if (> (caddr X) R) (car X) NIL)) (get This 'LetterAmounts))))
  183. (link X)
  184. ) ) )
  185. # (println "Trying" Lst (numVowels Lst))
  186. # check if the list is good
  187. (unless (areLettersOk Lst) (setq Lst NIL))
  188. )
  189.  
  190. # (println "Final" Lst (numVowels Lst))
  191. (put This 'LetterList Lst) ) )
  192.  
  193.  
  194. (dm makeTiles> ()
  195. (let ( Lst NIL
  196. SX (get This 'Sx)
  197. SY (get This 'Sy)
  198. RR (get This 'R) )
  199. (setq Lst
  200. (make
  201. (for (I . X) (get This 'LetterList)
  202. (link (new '(+Tile) (* SX (- I 1)) 0 X "yellow" "red" SX SY RR ) ) ) ) )
  203. (put This 'TileList Lst) ) )
  204.  
  205.  
  206. (dm init> (SX SY Radius)
  207. (let
  208. (N (get This 'Counts)
  209. L (get This 'Letters)
  210. S 0
  211. R (if (=0 Radius) (/ SX 6) Radius) )
  212. (put This 'Sx SX)
  213. (put This 'Sy SY)
  214. (put This 'R R)
  215. (put This 'LetterAmounts (make (mapcar '((X Y) (inc 'S Y) (link (cons X Y (cons S))) ) L N)))
  216. (set (car (prop This 'TotTiles)) (apply + N)) )
  217. (genLetters> This)
  218. (makeTiles> This) # one tile per letter
  219. )
  220.  
  221.  
  222. ################################################################################
  223. # G R I D T I L E M E T H O D S
  224. ################################################################################
  225.  
  226. # each grid tile is same size as a +Tile (sx, sy)
  227. # grid has 12 rows and 10 columns
  228. # row 1 is used for the initial tiles to be placed
  229. # row number 2 is unused
  230. # the grid is laid out as a 1d array
  231. # col1, col2, col3 ....
  232.  
  233. (de gridPxlToId (X Y) # X and Y are pixel locations with respect to *W and *H
  234. (let
  235. (I (/ X *Wt)
  236. J (/ Y *Ht)
  237. )
  238. (msg (glue "," (list X Y *Wt *Ht I J *Nc (+ I 1 (* *Nc J)))))
  239. (+ I 1 (* *Nc J)) ) )
  240.  
  241. ################################################################################
  242. # M A I N M E T H O D S
  243. ################################################################################
  244.  
  245. (de drawTiles ()
  246. (for (I . Obj) (get '+TileMgr 'TileList)
  247. (msg (glue " " (list "drawTiles " I " -> " (get Obj 'l))))
  248. (draw> Obj) ) )
  249.  
  250. (de drawImage ()
  251. (csDrawImage (sesId (pack "!mk.svg?" (ht:Fmt This *Cnt)))
  252. 0 # X
  253. 0 # Y
  254. ) )
  255.  
  256. (de drawCanvas (Id Dly F X Y X2 Y2)
  257. (msg "Draw canvas start")
  258. (inc *Cnt)
  259. (unless (= NIL F) (msg (glue "," (list F X Y X2 Y2 "G" (gridPxlToId X Y)))))
  260. (if *Upd (prog1 (make (drawImage)) (msg "Draw canvas done")) (prog1 NIL (msg "Draw canvas noop")))
  261. )
  262.  
  263.  
  264. # These two variables myst be set before canvasTest as it uses a read macro
  265. (setq *PctW 60)
  266. (setq *PctH 80)
  267.  
  268. (de setCanvasSize (W H)
  269. #(msg (glue " " (list "setCanvasSize" W H)))
  270. (setq *W W)
  271. (setq *H H) )
  272.  
  273. (de canvasTest ()
  274. (action
  275. (html 0 "Canvas Test1" "@lib.css"
  276. '(
  277. (onload .
  278. `(glue ";"
  279. (list
  280. "var form = document.getElementById('$formID')"
  281. # "alert('hi' + form.clientHeight)"
  282. "var cnvsDiv = document.getElementById('$canvasID')"
  283. `(glue "" (list "var cnvsWidth = \"width:\" + String(" *PctW " * 0.01 *form.clientWidth) + \"px;\" "))
  284. `(glue "" (list "var cnvsHeight = \"height:\" + String(" *PctH " * 0.01 * form.clientHeight) + \"px;\" "))
  285. "var cnvsCol = \"background-color: orange; padding: 10px; margin:10px;\""
  286. "var cnvsStyle = cnvsHeight + cnvsWidth + cnvsCol"
  287. `(glue "" (list "cnvsDiv.setAttribute(\"style\"," "cnvsStyle" ")"))
  288. "var cs = getComputedStyle(cnvsDiv)"
  289. "var widthPxls = parseInt(cs.getPropertyValue('width'), 10)"
  290. "var heightPxls = parseInt(cs.getPropertyValue('height'), 10)"
  291. "var canvas = document.getElementById('$testID')"
  292. `(glue "*" (list "canvas.width = widthPxls " 90 "0.01"))
  293. `(glue "*" (list "canvas.height = heightPxls " 90 "0.01"))
  294. "lisp(null, \"setCanvasSize\", canvas.width, canvas.height)"
  295. "lisp(null, 'console', '--> onload: draw canvas start', widthPxls, heightPxls, canvas.width, canvas.height, document.body.scrollHeight)"
  296. "drawCanvas('$testID', -1)"
  297. "lisp(null, 'console', '+++ onload: drawCanvas done')"
  298. ) ) )
  299. (onresize .
  300. `(glue ";"
  301. (list
  302. "var form = document.getElementById('$formID')"
  303. # "alert('hi' + form.clientHeight)"
  304. "var cnvsDiv = document.getElementById('$canvasID')"
  305. `(glue "" (list "var cnvsWidth = \"width:\" + String(" *PctW " * 0.01 *form.clientWidth) + \"px;\" "))
  306. `(glue "" (list "var cnvsHeight = \"height:\" + String(" *PctH " * 0.01 * form.clientHeight) + \"px;\" "))
  307. "var cnvsCol = \"background-color: orange; padding: 10px; margin:10px;\""
  308. "var cnvsStyle = cnvsHeight + cnvsWidth + cnvsCol"
  309. `(glue "" (list "cnvsDiv.setAttribute(\"style\"," "cnvsStyle" ")"))
  310. "var cs = getComputedStyle(cnvsDiv)"
  311. "var widthPxls = parseInt(cs.getPropertyValue('width'), 10)"
  312. "var heightPxls = parseInt(cs.getPropertyValue('height'), 10)"
  313. "var canvas = document.getElementById('$testID')"
  314. `(glue "*" (list "canvas.width = widthPxls " 90 "0.01"))
  315. `(glue "*" (list "canvas.height = heightPxls " 90 "0.01"))
  316. "lisp(null, \"setCanvasSize\", canvas.width, canvas.height)"
  317. "lisp(null, 'console', '---> onresize: draw canvas start', widthPxls, heightPxls, canvas.width, canvas.height, document.body.scrollHeight)"
  318. "drawCanvas('$testID', -1)"
  319. "lisp(null, 'console', '+++ onresize: drawCanvas done')"
  320. ) ) )
  321. )
  322. (form '((id . "$formID") (style . "position:absolute;top:5px; bottom:5px;left:5px;right:5px;background-color:blue"))
  323. ( <div> '((id . "$canvasID") (style . "background-color; orange;padding: 10px; margin:10px"))
  324. (<canvas> "$testID" "600" "800")
  325. )
  326. (<div> '((id . "$floatID") (width . "100%") (height . "20%") (style . "backgroundColor:green")))
  327. ) ) ) )
  328. <post>
  329. (de setup ()
  330. (setq *Upd 1)
  331. (setq *Cnt 0)
  332. (setq *W 600)
  333. (setq *H 800)
  334. (setq *Nr 12) # number of rows of grid tiles in grid
  335. (setq *Nc 10) # number of columns of grid tiles in grid
  336. (setq *Wt (/ *W *Nc)) # width of each tile
  337. (setq *Ht (/ *H *Nr)) # height of each tile
  338. (seed (time))
  339. (init> '+TileMgr 100 100 15) # virtual scale not actual pixels
  340.  
  341. # (createTiles)
  342. )
  343.  
  344. (de work ()
  345. (app)
  346. (setup)
  347. (redirect (baseHRef) *SesId "!canvasTest") )
  348.  
  349. (server 8080 "!work")
  350.  
  351. # ./pil draw2Rects.l +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement