-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemoShapes.htm
298 lines (243 loc) · 6 KB
/
demoShapes.htm
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
<html>
<head>
<title>loEngine Shape Demo</title>
<script src="lo/loMath.src.js"></script>
<script src="lo/loEngine.src.js"></script>
<script src="lo/loEngineDisp.src.js"></script>
<script type="application/javascript">
$ = function(e){ return document.getElementsByName(e)[0] || document.getElementById(e) }
// Event class
var loTick = loEngineEvents.create();
// Create new layer
var layer_1 = loContainer.newLayer( "Layer 1" )
// Create a point light (note: this remains a reference to it)
var myPointLight = loCreatePointLight( 0, 0 );
myPointLight.colour = "rgba(255,20,255,0)";
// We want to make it dynamic, so assign an angle property
myPointLight.angle = 250
// Push the light to the container
var lights = loContainer.findLayer( "__lights" )
if( lights ) lights.push( myPointLight )
function updateMyLight()
{
--myPointLight.angle
var lRadius = $('objLightRadius') ? Number($('objLightRadius').value) || 200 : 200
// Rotate around a radius of 200
var lightPos = loPolygon.makeVertex( myPointLight.angle, lRadius )
myPointLight.pos.x = 200 + lightPos.x
myPointLight.pos.y = 200 + lightPos.y
}
updateMyLight();
function draw()
{
var canvas = $('loDemo');
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#000";
ctx.strokeStyle = "#000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
loContainer.render( ctx, $('objDebug').checked )
//layer_1.render( ctx, $('objDebug').checked )
ctx.fillStyle = "#000";
ctx.beginPath();
ctx.fillRect( myPointLight.pos.x-1, myPointLight.pos.y-1, 2, 2 )
ctx.stroke()
}
function demoSetup()
{
cvs = $('loDemo');
loTick.add(
5,
function()
{
// Rotate light source
if( $('objRotateLight').checked )
{
updateMyLight();
}
// Rotate each object
if( $('objRotate').checked )
{
layer_1.eachObj
(
function( loObj )
{
loObj.rotate( loObj.angle +2 );
// there is only ONE object in here
//$('loDebugInfo').innerHTML = loObj
}
)
}
}
)
// new: flicker lights
loTick.add
(
20,
function()
{
if( lights )
{
var i = 0;
while( i < lights.objects.length )
{
lights.objects[ i ].powradius = 300 + ( loRand( -3 ) + 1.5 )
++i
}
}
}
)
loTick.add(
5,
function()
{
draw();
}
)
recreateShapes()
loTick.starti();
}
function addLight()
{
if( !( lights ) ) return;
lights.push
(
loCreatePointLight
(
Number( $('objLightX').value ),
Number( $('objLightY').value )
)
)
}
function addShape()
{
var tmp = loPolygon.create
(
Number($('objPosX').value),
Number($('objPosY').value),
Number($('objRadius').value),
loMakeNArray( $('objSides').value )
)
layer_1.push( tmp )
//$('loDebugInfo').innerHTML += tmp
}
function recreateShapes()
{
layer_1.clear();
tmp = loPolygon.create
(
200,
200,
$('objRadius').value,
loMakeNArray( $('objSides').value )
)
layer_1.push( tmp )
//$('loDebugInfo').innerHTML = tmp
}
function createRectangle()
{
layer_1.clear();
tmp = loCreateRect
(
200,
200,
$('objWidth').value,
$('objHeight').value,
$('objRadius').value
)
layer_1.push( tmp )
//$('loDebugInfo').innerHTML = tmp
}
function createCircle()
{
layer_1.clear();
tmp = loCreateCircle
(
200,
200,
$('objRadius').value
)
layer_1.push( tmp )
//$('loDebugInfo').innerHTML = tmp
}
function switchShadows()
{
lights.clear()
if( $('objSoftTest').checked ) myPointLight = loCreateTestLight( 0, 0 );
else myPointLight = loCreatePointLight( 0, 0 );
lights.push( myPointLight )
updateMyLight()
}
</script>
</head>
<body onload="demoSetup()">
loEngine v0.5
<hr>
<div>
<div style="float: left">
<canvas id="loDemo" width="400" height="400">
Sorry, your browser doesn't support canvas technology
</canvas>
</div>
<div style="float:left">
Use this form to create various regular polygons<br><br>
<form>
<table>
<tr>
<td>Properties</td>
<td>
<label><input type=radio name="objType" checked/> Polygon</label><br>
<!--<label><input type=radio name="objType" /> Circle</label>-->
</td>
</tr>
<tr>
<td>Sides</td>
<td><input type=text name="objSides" value="3" /> (3-n)
<a href="javascript:void( $('objSides').value=Math.round(Math.random()*10+3) );">random</a>
</td>
</tr>
<tr>
<td>Radius</td>
<td><input type=text name="objRadius" value="40" /></td>
</tr>
<tr>
<td>Rectangle</td>
<td>
<input type=text name="objWidth" value="50" /> (w) <br>
<input type=text name="objHeight" value="25" /> (h)
</td>
</tr>
</table>
<input type=button onClick="javascript:recreateShapes();" value="Generate">
<input type=button onClick="javascript:createRectangle();" value="Generate Rectangle">
<input type=button onClick="javascript:createCircle();" value="Generate Circle">
<br><br>
<div style="float:left">
<input type=text name="objPosX" value="200" /> (x) <br>
<input type=text name="objPosY" value="100" /> (y) <br>
<input type=button onClick="javascript:addShape();" value="Add Polygon">
</div>
<div style="float:right">
<input type=text name="objLightX" value="0" /> (x) <br>
<input type=text name="objLightY" value="400" /> (y) <br>
<input type=button onClick="javascript:addLight();" value="Add Light"> (blue)
</div>
<div style="clear:both"></div>
<br>
<input type=text name="objLightRadius" value="200" /> (light radius)
</form>
<p>
<label><input type=checkbox name="objDebug" checked /> Draw debug</label>
<label><input type=checkbox name="objRotate" /> Rotate shape</label>
<label><input type=checkbox name="objRotateLight" /> Rotate Light</label>
<br>
<b>Alpha soft shadows</b>
<label><input type=checkbox name="objSoftTest" onClick="javascript:switchShadows()" /> Enable</label> (only toggle once)
</p>
<!--<pre name='loDebugInfo'></pre>-->
</div>
</div>
<div style="clear:both"></div>
<hr>
</body>
</html>