-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.html
280 lines (223 loc) · 7.37 KB
/
canvas.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
<!DOCTYPE HTML>
<!--
written by Nick Shin - [email protected]
the code found in this file is licensed under:
- Unlicense - http://unlicense.org/
this file is from https://github.com/nickshin/CheatSheets/
this file contains some HTML5 snippets on:
- canvas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reference sources:
+ http://www.html5canvastutorials.com/
+ http://www.html5canvastutorials.com/advanced/html5-canvas-advanced-tutorials-introduction/
+ http://www.w3schools.com/html5/html5_canvas.asp
DEMOS!!!
+ http://www.queness.com/post/3885/8-simply-amazing-html5-canvas-and-javascript-animations
notes:
+ canvas coordinate origin is upper left
+ immediate mode
+ GPU support(?)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
best viewed in editor with tab stops set to 4
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
-->
<!-- ================================================== -->
<!-- START OF HTML -->
<html>
<head>
<title>Canvas : HTML5 test code</title>
<!-- ================================================== -->
<!-- Javascript {{{ -->
<!-- Canvas2D {{{2 -->
<!-- .................................................. -->
<script>
function canvas2D() {
var can = document.getElementById( 'canvas2D' );
var ctx = can.getContext( '2d' );
draw_image( ctx );
draw_pattern( ctx );
draw_instructions( ctx );
draw_gradient( ctx );
// iterate_pixel_data( ctx );
save_data_to_URL( can );
}
// draw_image() {{{3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function draw_image( ctx ) {
var smileyface = new Image();
smileyface.src = "images/smileyface.png";
smileyface.onload = function() {
// ctx.drawImage( smileyface , 350, 0 // position (x,y)
// , 100, 100 // scale to (w,h)
// );
ctx.drawImage( smileyface
, 50, 50 // clip (x,y)
, 50, 50 // clip (w,h)
, 350, 0 // position (x,y)
, 100, 100 // scale to (w,h)
);
// ctx.drawImage( img, posX, posY );
// ctx.drawImage( img, posX, posY, scaleW, scaleH );
// ctx.drawImage( img, clipX, clipY, clipW, clipH, posX, posY, scaleW, scaleH );
};
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// draw_image() }}}3
// draw_pattern() {{{3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function draw_pattern( ctx ) {
var woody = new Image();
woody.src = "images/wood-pattern.png";
woody.onload = function() {
var pattern = ctx.createPattern(woody, "repeat");
ctx.save();
ctx.rect(350, 225, 200, 100);
ctx.fillStyle = pattern;
ctx.fill();
ctx.restore();
};
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// draw_pattern() }}}3
// draw_instructions() {{{3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function draw_instructions( ctx ) {
// draw eyes
ctx.fillRect( 160, 130, 20, 80 );
// do the other eye the long and "advanced" way...
// ctx.fillRect( 220, 130, 20, 80 );
ctx.save();
ctx.rect( 220, 130, 20, 80 );
ctx.fillStyle = "#000000";
ctx.shadowColor = "#8ED6FF";
ctx.shadowBlur = 20;
ctx.shadowOffsetX = 15;
ctx.shadowOffsetY = 15;
ctx.fill();
ctx.restore();
// draw mouth
ctx.lineWidth = 20;
ctx.beginPath(); // start smile
ctx.moveTo( 100, 230 );
ctx.bezierCurveTo( 100, 230, 20, 380, 300, 230 );
// ctx.closePath();
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.strokeStyle = "#110000";
ctx.stroke();
ctx.beginPath(); // start tongue
ctx.moveTo( 219, 298 );
ctx.bezierCurveTo( 278, 351, 315, 315, 277, 258 );
// ctx.closePath();
ctx.fillStyle = "#880000";
ctx.fill();
ctx.strokeStyle = "#440000";
ctx.stroke();
// reset path so future fill() does not look at last path created
ctx.beginPath();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// draw_instructions() }}}3
// draw_gradient() {{{3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function draw_gradient( ctx ) {
var x0 = 50;
var y0 = 50;
var w = 175;
var h = 50;
var x1 = x0 + w;
var y1 = y0 + h
var grd=ctx.createLinearGradient(x0,y0,x1,y1);
// grd.addColorStop(0,"#FF0000");
// grd.addColorStop(1,"#00FF00");
grd.addColorStop(0, "red");
grd.addColorStop(0.17, "orange");
grd.addColorStop(0.33, "yellow");
grd.addColorStop(0.5, "green");
grd.addColorStop(0.666, "blue");
grd.addColorStop(1, "violet");
ctx.fillStyle=grd;
// ctx.fill(); // draws with bezierCurve, so don't use it here
ctx.fillRect(x0,y0,w,h);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// draw_gradient() }}}3
// iterate_pixel_data() {{{3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function iterate_pixel_data( ctx ) {
var imageData = ctx.getImageData(200, 350, 100, 100);
var data = imageData.data;
// to quickly iterate over all pixels, use a for loop like this
for (var i = 0; i < data.length; i += 4) {
// this will 'invert' the image data
data[i] = 255 - data[i]; // red
data[i + 1] = 255 - data[i + 1]; // green
data[i + 2] = 255 - data[i + 2]; // blue
// i+3 is alpha (the fourth element)
// this will 'grayscale' the image data
var brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
data[i] = brightness; // red
data[i + 1] = brightness; // green
data[i + 2] = brightness; // blue
// i+3 is alpha (the fourth element)
}
// overwrite original image
ctx.putImageData(imageData, 0, 0);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// iterate_pixel_data() }}}3
// save_data_to_URL() {{{3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function save_data_to_URL( can ) {
// save canvas image as data url (png format by default)
// NOTE: images in the canvas are NOT SAVED here...
var dataURL = can.toDataURL();
var el = document.getElementById( "test" );
el.innerHTML = dataURL;
// dataURL is a base64 encoded text which can be saved in a text file
// (as seen above) -- as well as shoved in an Image object
var can2 = document.getElementById( 'dataURL' );
var ctx = can2.getContext( '2d' );
load_data_from_URL( ctx, dataURL );
}
function load_data_from_URL( ctx, dataURL ) {
var imageObj = new Image();
imageObj.onload = function(){
ctx.drawImage(this, 0, 0);
};
imageObj.src = dataURL;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// save_data_to_URL() }}}3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</script>
<!-- .................................................. -->
<!-- Convas2D }}}2 -->
<!-- this document {{{2 -->
<!-- .................................................. -->
<script>
onload = onload_handler;
function onload_handler() {
canvas2D();
}
</script>
<!-- .................................................. -->
<!-- this document }}}2 -->
<!-- Javascript }}} -->
<!-- ================================================== -->
</head>
<!-- ================================================== -->
<!-- START OF BODY -->
<body>
<!-- .................................................. -->
<canvas id="canvas2D" width="600" height="400" style="border:1px solid #c3c3c3;">
Your browser doesn't support canvas.
</canvas>
<hr>
<canvas id="dataURL" width="600" height="400" style="border:1px solid #c3c3c3;">
Your browser doesn't support canvas.
</canvas>
<hr>
<div id="test">hi</div>
<!-- ================================================== -->
</body>
</html>