-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path004-canvas-resize.html
333 lines (220 loc) · 8.66 KB
/
004-canvas-resize.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Interactive Drawing - Canvas Resize</title>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0, user-scalable=no" />
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#canvas-wrapper {
width: 100%;
height: 100%;
position: absolute;
overflow: hidden;
}
canvas {
position: absolute;
background: #CCC;
}
.CSS3Animated {
/* enable harware acceleration */
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
-ms-transform: translate3d(0, 0, 0);
-o-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
/* animate */
-webkit-transition: all 300ms cubic-bezier(0.445, 0.045, 0.0355, 1);
-moz-transition: all 300ms cubic-bezier(0.445, 0.045, 0.0355, 1);
-ms-transition: all 300ms cubic-bezier(0.445, 0.045, 0.0355, 1);
-o-transition: all 300ms cubic-bezier(0.445, 0.045, 0.0355, 1);
transition: all 300ms cubic-bezier(0.445, 0.045, 0.0355, 1);
/* prevent flickering in animation */
-webkit-backface-visibility: hidden;
-webkit-perspective: 1000;
}
#settings {
padding: 20px 10px;
position: absolute;
}
a {
color: #000;
font-size: 14px;
font-family: Georgia;
padding: 10px;
text-decoration: none;
background: #F0F;
}
a:hover {
background: #0CC;
}
</style>
</head>
<body>
<div id="canvas-wrapper">
<canvas id="canvas">
<center>
<h1>Your browser doesn't support canvas ...</h1>
<p>Try a <a href="https://www.google.com/chrome">modern browser</a> and come back some other time.</p>
</center>
</canvas>
</div>
<div id="settings">
<a id="clear" href="#">Clear</a>
<a id="save" href="#">Save</a>
</div>
<script>
//
// MAIN SETUP ----------------------------------------------------------------
//
var canvas = document.getElementById('canvas'),
clear = document.getElementById('clear'),
save = document.getElementById('save'),
c = canvas.getContext('2d');
//
// 2D VECTOR CLASS -----------------------------------------------------------
// 2D vectors describe a point by it's xy-coordinate
//
var Vec2 = function(x, y) {
this.x = x >> 0 || 0;
this.y = y >> 0 || 0;
};
Vec2.prototype =
{
};
//
// STROKE CLASS --------------------------------------------------------------
// Each stroke is an array of Vec2-points
//
var Stroke = function() {
};
Stroke.prototype = {
};
//
// UTILITIES -----------------------------------------------------------------
//
var Utils = {};
// Debounce method to guarantee that a callback method will only ever
// be executed once in a given timeframe (<-> browser resize event in IE which fires continuously)
// http://www.paulirish.com/2009/throttled-smartresize-jquery-event-handler
// http://davidwalsh.name/function-debounce
// Note: if Underscore/Lo-Dash is included in your project you can use _.debounce(callback, wait)
// http://lodash.com/docs#debounce
Utils.debounce = function(callback, wait) {
var timeout = null;
return function() {
var obj = this, args = arguments;
var delayed = function() {
callback.apply(obj, args);
timeout = null;
};
if(timeout) clearTimeout(timeout);
timeout = setTimeout(delayed, wait);
};
};
// Get CSS3 support
Utils.checkCSS3Support = function(prop) {
var vendors = ['webkit', 'Moz', 'O', 'ms', ''], i = vendors.length;
while(i--) if((vendors[i] + prop) in document.body.style && !Utils.isIE9()) return vendors[i] + prop;
return false;
};
// Check if we're on IE9
// IE9 has CSS3 properties but 'transitions' are not supported
// http://quirksmode.org/js/detect.html
Utils.isIE9 = function() {
var mode = document.documentMode, lang = window.navigator.systemLanguage;
return lang && ( !mode || mode === 9 ) ? true : false;
};
//
// APPLICATION ---------------------------------------------------------------
//
var App = (function() {
var size, position, pixelRatio, retinaSize, CSS3Transform = Utils.checkCSS3Support('Transform');
// INIT
var init = function() {
// Our canvas is a square taking the biggest side as its base
size = screen.width < screen.height ? screen.height : screen.width;
size /= 4;
// Enable retina support by scaling the canvas x 2,
// while setting the CSS property to its normal size (the img-trick)
// For this we need to ge the pixelratio of the device
// On retina devices this has a value of 2, setting it to 1.99 however speeds up drawing
pixelRatio = window.devicePixelRatio && window.devicePixelRatio === 2 ? 1.99 : 1;
// Now, let's set the canvas size
// Remember: the canvas is a square, taking its biggest size as its base x retina
// To support retina we need to update the CSS-values of the canvas
canvas.width = size * pixelRatio;
canvas.height = size * pixelRatio;
canvas.style.width = size +'px';
canvas.style.height = size +'px';
// Set canvas position
position = new Vec2();
// When scaling-up the canvas, all touch-positions will be wrong
// We can fix this by calculating touch-positions against the actual position
actualPosition = new Vec2();
// Set canvas position
this.resize();
// Add CSS3 animations after the first resize-event
setTimeout(function() { canvas.className = 'CSS3Animated'; }, 1000);
};
// RESIZE WINDOW
// Instead of auto-clearing the canvas onResize
// We move the entire canvas to a new position
var resize = function() {
// Center the canvas
position.x = (window.innerWidth - size) * .5 >> 0;
position.y = (window.innerHeight - size) * .5 >> 0;
// Keep track of its real position
// to calculate touch-positions against on retina-devices
actualPosition.x = position.x * pixelRatio;
actualPosition.y = position.y * pixelRatio;
// Set canvas position
// Use CSS3 Translation for hardware acceleration
// Note: not supported on Opera Mini - http://caniuse.com/#search=translate
if(CSS3Transform) canvas.style[CSS3Transform] = 'translate3d('+ position.x +'px, '+ position.y +'px, 0)';
else {
canvas.style.left = position.x +'px';
canvas.style.top = position.y +'px';
}
};
//
// PUBLIC API ------------------------------------------------------------
//
return {
getActualPosition : function() { return actualPosition; },
getPixelRatio : function() { return pixelRatio; },
init : init,
resize : resize
}
}());
//
// EVENT MANAGER -------------------------------------------------------------
//
var Events = (function() {
//
// TOUCH DEVICES ---------------------------------------------------------
//
if('ontouchstart' in document) {
// BROWSER RESIZE
// Add a default resize event as well
// in case window.orientation is not supported
// http://davidwalsh.name/orientation-change
if(window.orientation) window.onorientationchange = Utils.debounce(App.resize, 100);
else window.onresize = Utils.debounce(App.resize, 100);
//
// DESKTOP ---------------------------------------------------------------
//
} else {
// BROWSER RESIZE
window.onresize = Utils.debounce(App.resize, 100);
}
}());
// START THE APPLICATION
App.init();
</script>
</body>
</html>