-
Notifications
You must be signed in to change notification settings - Fork 2
/
pixelated-2d.html
140 lines (113 loc) · 4.43 KB
/
pixelated-2d.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
<html>
<head>
<script>
var offscreenCanvas = null;
var canvas2DOffscreen = null;
var canvas2DOnscreen = null;
var contextOffscreen = null;
var contextOnscreen = null;
let parametricEval = function(x, a, b, c, d, e, f, g) {
const absX = Math.abs(x);
const sgnX = Math.sign(x);
return sgnX * (absX <= d) ? c*absX + f : Math.pow(a*absX + b, g) + e;
}
let srgbToLinear = function(x) {
return parametricEval(
x, 1/1.055, 0.055/1.055, 1/12.92, 0.04045, 0, 0, 2.4);
}
let linearToSrgb = function(x) {
return parametricEval(
x, Math.pow(1.055, 2.4), 0, 12.92, 0.04045 / 12.92, -0.055, 0, 1/2.4);
}
async function draw() {
canvasDraw2D(context2DOffscreen);
contextIBROffscreen.transferFromImageBitmap(canvas2DOffscreen.transferToImageBitmap());
canvasDraw2D(context2DOffscreen);
contextIBROnscreen.transferFromImageBitmap(canvas2DOffscreen.transferToImageBitmap());
canvasDraw2D(context2DOffscreen);
canvasDraw2D(context2DOnscreen);
}
async function initGpuCanvas() {
canvas2DOffscreen = Canvas2DOffscreen.transferControlToOffscreen();
context2DOffscreen = canvas2DOffscreen.getContext('2d');
canvas2DOffscreen.width = 8;
canvas2DOffscreen.height = 8;
canvas2DOnscreen = Canvas2DOnscreen;
context2DOnscreen = canvas2DOnscreen.getContext('2d');
canvas2DOnscreen.width = 8;
canvas2DOnscreen.height = 8;
canvasIBROffscreen = CanvasIBROffscreen.transferControlToOffscreen();
contextIBROffscreen = canvasIBROffscreen.getContext('bitmaprenderer');
canvasIBROffscreen.width = 8;
canvasIBROffscreen.height = 8;
canvasIBROnscreen = CanvasIBROnscreen;
contextIBROnscreen = canvasIBROnscreen.getContext('bitmaprenderer');
canvasIBROnscreen.width = 8;
canvasIBROnscreen.height = 8;
draw();
}
let updateImageRendering = function() {
Canvas2DOffscreen.style.imageRendering = ImageRenderingAuto.checked ? 'auto' : 'pixelated';
Canvas2DOnscreen.style.imageRendering = ImageRenderingAuto.checked ? 'auto' : 'pixelated';
CanvasIBROffscreen.style.imageRendering = ImageRenderingAuto.checked ? 'auto' : 'pixelated';
CanvasIBROnscreen.style.imageRendering = ImageRenderingAuto.checked ? 'auto' : 'pixelated';
}
async function canvasDraw2D(context) {
if (!context) {
return;
}
context.fillStyle = 'red';
context.fillRect( 0, 0, 4, 4);
context.fillStyle = 'green';
context.fillRect( 0, 4, 4, 4);
context.fillStyle = 'blue';
context.fillRect(4, 0, 4, 4);
let color = 'rgb(' +
Math.round(100 * Number(PixelValueSlider.value)) + '%,' +
Math.round(100 * Number(PixelValueSlider.value)) + '%,' +
Math.round(100 * Number(PixelValueSlider.value)) + '%)';
console.log(color);
context.fillStyle = color;
context.fillRect(4,4,4, 4);
}
let updatePixelValue = function() {
// Update pixel value text.
PixelValue.innerHTML = Number(PixelValueSlider.value).toFixed(2);;
PixelValueLinear.innerHTML = srgbToLinear(PixelValueSlider.value).toFixed(2);
draw();
}
function main() {
// Configure rendering slider.
PixelValueSlider.addEventListener('input', updatePixelValue, false);
initGpuCanvas();
}
</script>
</head>
<body onload='main()'>
<h1>Image rendering bugs</h1>
<h2>Rendering settings</h2>
<p>
Pixel value:
<input id="PixelValueSlider" type="range" min="0" max="3" step="0.01" value="1" list="PixelValueSliderTicks""/>
<datalist id="PixelValueSliderTicks">
<option value="0" label="0"></option>
<option value="1" label="1"></option>
<option value="2" label="2"></option>
<option value="3" label="3"></option>
</datalist>
<tt id="PixelValue"></tt> (sRGB-encoded), <tt id="PixelValueLinear">?</tt> (linear-encoded)
</p>
<p>
Image rendering:
<input type="radio" name="ImageRendering" id="ImageRenderingAuto" onchange="updateImageRendering();" checked>
<label for="ImageRenderingAuto"><tt>auto</tt></label>
<input type="radio" name="ImageRendering" id="ImageRenderingPixelated" onchange="updateImageRendering();">
<label for="ImageRenderingPixelated"><tt>pixelated</tt></label>
</p>
<h2>2D canvas (offscreen, onscreen)</h2>
<canvas id="Canvas2DOffscreen" style="width:128px; height:128px; border:1px solid black;"></canvas>
<canvas id="Canvas2DOnscreen" style="width:128px; height:128px; border:1px solid black;"></canvas>
<h2>Image bitmap rendering canvas (offscreen, onscreen)</h2>
<canvas id="CanvasIBROffscreen" style="width:128px; height:128px; border:1px solid black;"></canvas>
<canvas id="CanvasIBROnscreen" style="width:128px; height:128px; border:1px solid black;"></canvas>
</html>