testDrawImage-1.html
1 |
<!DOCTYPE html>
|
---|---|
2 |
<html>
|
3 |
<head>
|
4 |
<title>Test</title> |
5 |
</head>
|
6 |
<body>
|
7 |
<canvas id="canvas"></canvas> |
8 |
<canvas id="canvas2"></canvas> |
9 |
<script type="text/javascript"> |
10 |
var me = {};
|
11 |
|
12 |
me.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
|
13 |
|
14 |
/**
|
15 |
* Returns the Chrome version object if it is applicable, otherwise undefined value.
|
16 |
*
|
17 |
* @return The Chrome version object filled with major, minor, build and patch numbers.
|
18 |
*/
|
19 |
function getChromeVersion ()
|
20 |
{
|
21 |
var parts = navigator.userAgent.match(/Chrom(?:e|ium)\/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/);
|
22 |
|
23 |
if (parts == null || parts.length != 5) {
|
24 |
return undefined;
|
25 |
}
|
26 |
|
27 |
parts = parts.map(part => parseInt(part, 10));
|
28 |
|
29 |
return {
|
30 |
major: parts[1],
|
31 |
minor: parts[2],
|
32 |
build: parts[3],
|
33 |
patch: parts[4]
|
34 |
};
|
35 |
}
|
36 |
|
37 |
/** The Chrome version */
|
38 |
me.chromeVersion = getChromeVersion();
|
39 |
|
40 |
/**
|
41 |
* Creates an offscreen canvas.
|
42 |
*
|
43 |
* @returns The new created offscreen canvas
|
44 |
*/
|
45 |
function createOffscreenCanvas()
|
46 |
{
|
47 |
/** The off screen canvas to be used to make batch drawing. */
|
48 |
/*if (me.isChrome && me.chromeVersion.major >= 77)
|
49 |
{
|
50 |
return new OffscreenCanvas(1, 1);
|
51 |
}*/
|
52 |
|
53 |
return document.createElement('canvas');
|
54 |
}
|
55 |
|
56 |
/** Defines a method to create an offscreen canvas.*/
|
57 |
me.createOffscreenCanvas = createOffscreenCanvas;
|
58 |
|
59 |
/**
|
60 |
* Tests the screen media if it has a high resolution.
|
61 |
*
|
62 |
* @returns {Boolean}
|
63 |
* True if the screen media has a high resolution.
|
64 |
*/
|
65 |
function isHighResolution()
|
66 |
{
|
67 |
return window.matchMedia &&
|
68 |
(window.matchMedia(
|
69 |
'only screen and (min-resolution: 192dpi), only screen and ' +
|
70 |
'(min-resolution: 2dppx), ' +
|
71 |
'only screen and (min-resolution: 75.6dpcm)').matches ||
|
72 |
window.matchMedia(
|
73 |
'only screen and (-webkit-min-device-pixel-ratio: 2), only screen and ' +
|
74 |
'(-o-min-device-pixel-ratio: 2/1), only screen and ' +
|
75 |
'(min--moz-device-pixel-ratio: 2), only screen and ' +
|
76 |
'(min-device-pixel-ratio: 2)').matches);
|
77 |
}
|
78 |
|
79 |
/** Tests the screen media if it has a high resolution.*/
|
80 |
me.isHighResolution = isHighResolution;
|
81 |
|
82 |
|
83 |
|
84 |
// Set display size (css pixels).
|
85 |
var size = 200;
|
86 |
|
87 |
var offscreenCanvas = me.createOffscreenCanvas();
|
88 |
|
89 |
var scale = 1;
|
90 |
|
91 |
if (me.isHighResolution())
|
92 |
{
|
93 |
scale = 2;
|
94 |
}
|
95 |
|
96 |
|
97 |
|
98 |
var offscreenCtx = offscreenCanvas.getContext('2d', {'alpha' : true});
|
99 |
|
100 |
// setup canvas
|
101 |
offscreenCanvas.style.width = size + "px";
|
102 |
offscreenCanvas.style.height = size + "px";
|
103 |
|
104 |
|
105 |
// Set actual size in memory (scaled to account for extra pixel density).
|
106 |
offscreenCanvas.width = size * scale;
|
107 |
offscreenCanvas.height = size * scale;
|
108 |
// Normalize coordinate system to use css pixels.
|
109 |
offscreenCtx.scale(scale, scale);
|
110 |
|
111 |
|
112 |
function drawOn(ctx0, scale0, width0, height0, size0)
|
113 |
{
|
114 |
// draw on offscreenCanvas
|
115 |
ctx0.fillStyle = "#bada55";
|
116 |
|
117 |
ctx0.fillRect(10, 10, size0 - 20, size0 - 20);
|
118 |
|
119 |
ctx0.fillStyle = "#cbeb66";
|
120 |
for (var i = 1; i <= (size0 / 10); i++)
|
121 |
{
|
122 |
ctx0.fillRect((i-1)*10, (i-1)*10, 10, 10);
|
123 |
}
|
124 |
ctx0.fillStyle = "#ffffff";
|
125 |
ctx0.font = '18px Arial';
|
126 |
ctx0.textAlign = 'center';
|
127 |
ctx0.textBaseline = 'middle';
|
128 |
|
129 |
var x = size0 / 2;
|
130 |
var y = size0 / 2;
|
131 |
|
132 |
var textString = "Don't do that until dread";
|
133 |
ctx0.fillText(textString, x, y);
|
134 |
|
135 |
ctx0.strokeStyle = "#0000ff";
|
136 |
|
137 |
ctx0.strokeRect(0, 0, size0, size0);
|
138 |
|
139 |
var horizontal = ctx0.createImageData(width0, 10 * scale0);
|
140 |
var bimg = ctx0.getImageData(0, y * scale0, width0, 10 * scale0);
|
141 |
// Iterate through every pixel
|
142 |
for (let i = 0; i < horizontal.data.length; i += 4)
|
143 |
{
|
144 |
// Modify pixel data
|
145 |
horizontal.data[i + 0] = 190 ^ bimg.data[i +0]; // R value
|
146 |
horizontal.data[i + 1] = 0 ^ bimg.data[i +1]; // G value
|
147 |
horizontal.data[i + 2] = 210 ^ bimg.data[i +2]; // B value
|
148 |
horizontal.data[i + 3] = 255; // A value
|
149 |
}
|
150 |
|
151 |
ctx0.putImageData(horizontal, 0, y * scale0);
|
152 |
|
153 |
bimg = ctx0.getImageData(x * scale0, 0, 10 * scale0, height0);
|
154 |
|
155 |
var vertical = ctx0.createImageData(10 * scale0, height0);
|
156 |
|
157 |
// Iterate through every pixel
|
158 |
for (let i = 0; i < vertical.data.length; i += 4)
|
159 |
{
|
160 |
// Modify pixel data
|
161 |
vertical.data[i + 0] = 190 ^ bimg.data[i +0]; // R value
|
162 |
vertical.data[i + 1] = 0 ^ bimg.data[i +1]; // G value
|
163 |
vertical.data[i + 2] = 210 ^ bimg.data[i +2]; // B value
|
164 |
vertical.data[i + 3] = 255; // A value
|
165 |
}
|
166 |
|
167 |
ctx0.putImageData(vertical, x * scale0, 0);
|
168 |
};
|
169 |
|
170 |
drawOn(offscreenCtx, scale, offscreenCanvas.width, offscreenCanvas.height, size);
|
171 |
|
172 |
|
173 |
var image = offscreenCtx.getImageData(0, 0, offscreenCanvas.width, offscreenCanvas.height);
|
174 |
|
175 |
var canvas = document.getElementById('canvas');
|
176 |
|
177 |
// setup canvas
|
178 |
canvas.style.width = size + "px";
|
179 |
canvas.style.height = size + "px";
|
180 |
|
181 |
canvas.width = size * scale;
|
182 |
canvas.height = size * scale;
|
183 |
|
184 |
var ctx = canvas.getContext('2d', {'alpha' : true});
|
185 |
// Normalize coordinate system to use css pixels.
|
186 |
ctx.scale(scale, scale);
|
187 |
|
188 |
ctx.putImageData(image, 0, 0);
|
189 |
|
190 |
|
191 |
// draw on canvas2 directly without scaling
|
192 |
|
193 |
var canvas2 = document.getElementById('canvas2');
|
194 |
|
195 |
// setup canvas
|
196 |
canvas2.style.width = size + "px";
|
197 |
canvas2.style.height = size + "px";
|
198 |
|
199 |
canvas2.width = size;
|
200 |
canvas2.height = size;
|
201 |
|
202 |
var ctx2 = canvas2.getContext('2d', {'alpha' : true});
|
203 |
drawOn(ctx2, 1, canvas2.width, canvas2.height, size);
|
204 |
|
205 |
</script>
|
206 |
|
207 |
</body>
|
208 |
</html>
|