-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas_view.cljs
59 lines (56 loc) · 2.33 KB
/
canvas_view.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(ns lambdaisland.daedalus.canvas-view
"Visualize your mesh and paths for easy debugging"
(:require ["hxdaedalus-js" :as daedalus]
[lambdaisland.daedalus :as dae]
[applied-science.js-interop :as j]))
;; Sadly the hxDaedalus-js build is borked, it was built with a missing
;; compile-time constant, so the JS code contains `null.split(...)`. So we redo
;; the constructor, then copy over the prototype.
(defn BasicCanvas [{:keys [width height framerate background-color]
:or {width 600
height 400
framerate 60
background-color "#FFFFFF"}}]
(this-as this
(let [^js canvas (js/window.document.createElement "canvas")
^js style (j/get canvas :style)
^js surface (.getContext canvas "2d" nil)
^js css (js/window.document.createElement "style")]
(j/assoc! canvas
:width width
:height height)
(j/assoc! this
:canvas canvas
:dom canvas
:image canvas
:body js/window.document.body
:surface surface
:style style
:header (j/lit {:width width
:height height
:frameRate framerate
:bgColor background-color}))
(j/assoc! style
:paddingLeft "0px"
:paddingTop "0px"
:left "0px"
:right "0px"
:position "absolute"
:backgroundColor background-color)
(j/assoc! css
:innerHTML "@keyframes spin { from { transform:rotate( 0deg ); } to { transform:rotate( 360deg ); } }"
:animation "spin 1s linear infinite")
(.appendChild ^js (first (js/window.document.getElementsByTagName "head"))
css)
(.loop this framerate)
(.appendChild ^js js/window.document.body canvas)
this)))
(j/extend! (.-prototype BasicCanvas)
(.-prototype (if daedalus/wings
daedalus/wings.jsCanvas.BasicCanvas
js/wings.jsCanvas.BasicCanvas)))
(defn canvas-view
([]
(canvas-view {} {}))
([canvas-opts view-opts]
(dae/simple-view (BasicCanvas. canvas-opts view-opts) view-opts)))