-
Notifications
You must be signed in to change notification settings - Fork 2
/
canvas.cljs
313 lines (256 loc) · 9.58 KB
/
canvas.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
(ns
^{:doc "Functions for building and manipulating the canvas DOM element"}
infinitelives.pixi.canvas
(:require [cljs.core.async :refer [<!]]
[infinitelives.utils.events :as events]
[infinitelives.utils.dom :as dom]
[infinitelives.utils.console :refer [log]]
[cljsjs.pixi])
(:require-macros [cljs.core.async.macros :refer [go]])
)
(def canvas-store (atom {}))
(defn add!
([key canvas]
(swap! canvas-store assoc key canvas))
([canvas]
(add! :default canvas)))
(defn set-default-once!
[canvas]
(when-not (:default @canvas-store)
(add! canvas)))
(defn remove!
([key]
(swap! canvas-store dissoc key)))
(defn get
([key]
(key @canvas-store))
([]
(:default @canvas-store)))
(defn make
"make a new pixi canvas, or initialise pixi with an existing canvas.
Pass in...
:expand if true makes the canvas take the entire window
:engine can be :webgl :canvas or :auto (default :auto)
and either:
:canvas a DOM element to use as the canvas
or:
:x x position for the new canvas
:y y position for the new canvas
:width width of new canvas
:height height of new canvas"
[{:keys [expand x y width height canvas engine background]
:or {expand false
x 0
y 0
width 800
height 600
background 0x500000
engine :auto}}]
(let [fswidth (.-innerWidth js/window)
fsheight (.-innerHeight js/window)
;; arguments for renderer
wid (if expand fswidth width)
hig (if expand fsheight height)
opts #js {"view" canvas
"transparent" false
"antialias" false
"preserveDrawingBuffer" false
"resolution" 1
"clearBeforeRender" true
"autoResize" false
"imageSmoothingEnabled" false
"backgroundColor" background
}
;; make the renderer
rend (case engine
:webgl (js/PIXI.WebGLRenderer. wid hig opts)
:canvas (js/PIXI.CanvasRenderer. wid hig opts)
(js/PIXI.autoDetectRenderer. wid hig opts))
;; details of the generated renderer
actual-canvas (.-view rend)
canvas-width (.-width actual-canvas)
canvas-height (.-height actual-canvas)]
(when-not canvas
;; custom canvas was generated. we should position it
;; and add it to the DOM
(do
(dom/set-style! actual-canvas
:left (if expand 0 x)
:top (if expand 0 y)
:position "absolute")
(dom/append! (.-body js/document) actual-canvas)))
(let [wind-width (if expand fswidth canvas-width)
wind-height (if expand fsheight canvas-height)
middle-x (Math/round (/ wind-width 2))
middle-y (Math/round (/ wind-height 2))]
(.resize rend wind-width wind-height))
;; return canvas and pixi renderer
{
:renderer rend
:canvas (or canvas actual-canvas)
}))
(defn make-stage
"Layout the stage structure"
[{:keys [layers origins
]
:or {
layers [:backdrop :below :world :above :ui :effect]
origins {}}}]
;(.log js/console (str layers))
(let [
stage (js/PIXI.Container.)
containers (map #(js/PIXI.Container.) layers)
;; _ (doall (map #(.addChild stage %) containers))
]
{
:stage stage
:origins origins
:layers layers
:layer
(into {}
(for [[k v] (partition 2 (interleave layers containers))]
[k v]))}))
(defn- center-container! [canvas layer edge]
(let [canvas-width (.-width canvas)
canvas-height (.-height canvas)
middle-x (Math/round (/ canvas-width 2))
middle-y (Math/round (/ canvas-height 2))]
;(log (str "w:" canvas-width " h:" canvas-height))
(case edge
:center
(do
;; start with world centered
(set! (.-position.x layer) middle-x)
(set! (.-position.y layer) middle-y))
:top
(do
(set! (.-position.x layer) middle-x)
(set! (.-position.y layer) 0))
:bottom
(do
(set! (.-position.x layer) middle-x)
(set! (.-position.y layer) canvas-height))
:left
(do
(set! (.-position.x layer) 0)
(set! (.-position.y layer) middle-y))
:right
(do
(set! (.-position.x layer) canvas-width)
(set! (.-position.y layer) middle-y))
:top-left
(do
(set! (.-position.x layer) 0)
(set! (.-position.y layer) 0))
:top-right
(do
(set! (.-position.x layer) canvas-width)
(set! (.-position.y layer) 0))
:bottom-left
(do
(set! (.-position.x layer) 0)
(set! (.-position.y layer) canvas-height))
:bottom-right
(do
(set! (.-position.x layer) canvas-width)
(set! (.-position.y layer) canvas-height))
;;default
(do
;; default layer is centered
(set! (.-position.x layer) middle-x)
(set! (.-position.y layer) middle-y)))))
(defn init
"Initialise the canvas element. Pass in optional keys
:background background colour (default 0x000000)
:expand if true makes the canvas take the entire window
:engine can be :webgl :canvas or :auto (default :auto)
:layers A list of keywords to refer to layers, from bottom to top
:origins A mapping of layer names to their origin positions. Default
position is center. Positions can be :center :top :bottom
:left :right :top-left :top-right :bottom-left :bottom-right
and either:
:canvas a DOM element to use as the canvas
or:
:x x position for the new canvas
:y y position for the new canvas
:width width of new canvas
:height height of new canvas
"
[opts]
(let [{:keys [renderer canvas layer layers stage
origins fullscreen-button] :or {origins {}} :as world}
(into (make opts)
(make-stage opts))]
;; add the stages to the canvas
(doall
(map
(fn [[name layer-obj]]
;(log "adding to:" (str stage) " layer:" (str layer-obj))
(.addChild stage layer-obj)
(center-container! canvas layer-obj (or (origins name) :center)))
layer))
;; do the first render
;(doall (for [l layers] (.render renderer (l layer))))
(.render renderer stage)
(let [
render-fn #(.render renderer stage)
resize-fn (fn [width height]
(.resize renderer width height)
(doall (map (fn [[name layer-obj]]
(center-container! canvas layer-obj
(or (origins name) :center)))
layer)))
expand-fn (fn [] (resize-fn (.-innerWidth js/window)
(.-innerHeight js/window)))
;; remember: fullscreen call will only work when
;; its called from a gesture. Like an on-click.
fullscreen-fn (fn [fullscreen]
(if fullscreen
(cond
(.-requestFullscreen canvas)
(.requestFullscreen canvas)
(.-webkitRequestFullscreen canvas)
(.webkitRequestFullscreen canvas)
(.-mozRequestFullScreen canvas)
(.mozRequestFullScreen canvas))
(cond
(.-exitFullscreen js/document)
(.exitFullscreen js/document)
(.-webkitExitFullscreen js/document)
(.webkitExitFullscreen js/document)
(.-msExitFullscreen js/document)
(.msExitFullscreen js/document)
(.-mozCancelFullScreen js/document)
(.mozCancelFullScreen js/document))))
resizer-loop
(when (:expand opts) (let [c (events/new-resize-chan)]
(go (while true
(let [[width height] (<! c)]
(resize-fn width height)
(render-fn))))))]
;; setup the render loop
(defn render []
(events/request-animation-frame render)
(render-fn))
(render)
(fullscreen-fn)
(let [canvas (into
world
{
:render-fn render-fn
:resize-fn resize-fn
:fullscreen-fn fullscreen-fn
:expand-fn expand-fn})]
(set-default-once! canvas)
canvas))))
(defn add-fullscreen-button! [{:keys [fullscreen-fn]}]
(let [div (dom/create-element :div)
img (dom/create-element :img)]
(dom/append! div img)
(set! (.-src img) "http://runrunitshim.com/images/fullscreenIcon.png")
(.setAttribute img "style" "bottom: 0; position: absolute; padding-bottom: 0px; padding-left: 0px; z-index: 200;")
(.setAttribute div "style" "bottom: 0; position: absolute; padding-bottom: 0px; padding-left: 0px; z-index: 200;")
(.addEventListener img "click" #(fullscreen-fn true))
(dom/append! (.-body js/document) div)
div
))