forked from ant512/CanvasLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvaslayers.html
152 lines (115 loc) · 3.93 KB
/
canvaslayers.html
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
<html>
<head>
<title>Canvas Layers</title>
<script type="application/javascript" src="canvaslayers.js"></script>
<script type="application/javascript">
var container;
var layer1;
var layer2;
var layer3child1;
var timer;
var layer1dx = 1;
var layer1dy = 2;
var layer2dx = 2;
var layer2dy = -1;
var layer31dx = 1;
var layer31dy = 2;
function init(canvasId) {
// Layer setup
var canvas = document.getElementById(canvasId);
container = new CanvasLayers.Container(canvas, true);
layer1 = new CanvasLayers.Layer(10, 60, 60, 60);
container.children.add(layer1);
var layer1child1 = new CanvasLayers.Layer(0, 0, 30, 30);
layer1.children.add(layer1child1);
layer2 = new CanvasLayers.Layer(20, 20, 100, 100);
container.children.add(layer2);
var layer3 = new CanvasLayers.Layer(50, 50, 200, 200);
layer3.setPermeable(true);
container.children.add(layer3);
layer3child1 = new CanvasLayers.Layer(0, 0, 60, 60);
layer3.children.add(layer3child1);
var layer3child2 = new CanvasLayers.Layer(0, 90, 200, 20);
layer3.children.add(layer3child2);
layer1.raiseToTop();
// Layer rendering methods
container.onRender = function(layer, rect, context) {
context.fillStyle = '#fff';
context.fillRect(0, 0, 400, 400);
}
layer1child1.onRender = function(layer, rect, context) {
context.fillStyle = '#0f0';
context.fillRect(0, 0, 400, 400);
context.strokeStyle = '#000'
context.strokeRect(0, 0, layer.getWidth(), layer.getHeight())
}
layer1.onRender = function(layer, rect, context) {
context.fillStyle = 'rgba(10, 10, 10, 0.5)';
context.fillRect(10, 10, layer.getWidth(), layer.getHeight());
context.strokeStyle = '#000'
context.strokeRect(0, 0, layer.getWidth(), layer.getHeight())
}
layer2.onRender = function(layer, rect, context) {
context.strokeStyle = '#000'
context.strokeRect(0, 0, layer.getWidth(), layer.getHeight());
context.fillStyle = '#00f';
context.fillRect(0, 0, layer.getWidth(), layer.getHeight());
}
layer3.onRender = function(layer, rect, context) {
context.fillStyle = '#ff0';
context.fillRect(0, 0, 400, 400);
context.strokeStyle = '#000'
context.strokeRect(0, 0, layer.getWidth(), layer.getHeight())
}
layer3child1.onRender = function(layer, rect, context) {
context.fillStyle = '#0ff';
context.fillRect(0, 0, layer.getWidth(), layer.getHeight());
}
layer3child2.onRender = function(layer, rect, context) {
context.fillStyle = '#f00';
context.fillRect(0, 0, layer.getWidth(), layer.getHeight());
}
timer = setInterval("changeScene()", 10);
}
function changeScene() {
layer1.moveTo(layer1.getRelativeX() + layer1dx, layer1.getRelativeY() + layer1dy);
if (layer1.getRelativeX() + layer1.getWidth() >= container.getWidth()) {
layer1dx = -1;
} else if (layer1.getRelativeX() <= 0) {
layer1dx = 1;
}
if (layer1.getRelativeY() + layer1.getHeight() >= container.getHeight()) {
layer1dy = -2;
} else if (layer1.getRelativeY() <= 0) {
layer1dy = 2;
}
layer2.moveTo(layer2.getRelativeX() + layer2dx, layer2.getRelativeY() + layer2dy);
if (layer2.getRelativeX() + layer2.getWidth() >= container.getWidth()) {
layer2dx = -2;
} else if (layer2.getRelativeX() <= 0) {
layer2dx = 2;
}
if (layer2.getRelativeY() + layer2.getHeight() >= container.getHeight()) {
layer2dy = -1;
} else if (layer2.getRelativeY() <= 0) {
layer2dy = 1;
}
layer3child1.moveTo(layer3child1.getRelativeX() + layer31dx, layer3child1.getRelativeY() + layer31dy);
if (layer3child1.getRelativeX() >= layer3child1.getParent().getWidth()) {
layer31dx = -1;
} else if (layer3child1.getRelativeX() <= -layer3child1.getWidth()) {
layer31dx = 1;
}
if (layer3child1.getRelativeY() >= layer3child1.getParent().getHeight()) {
layer31dy = -2;
} else if (layer3child1.getRelativeY() <= -layer3child1.getHeight()) {
layer31dy = 2;
}
container.redraw();
}
</script>
</head>
<body onload="init('canvas')">
<canvas id="canvas" width="400" height="400"></canvas>
</body>
</html>