-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
214 lines (184 loc) · 5.62 KB
/
example.js
File metadata and controls
214 lines (184 loc) · 5.62 KB
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
import { far } from "../src/index.js";
function getReferenceContext2d(element, transform) {
const context = element.getContext("2d");
// Reset transform first
context.resetTransform();
// Then apply in same order: scale, rotate, translate
context.scale(transform.scale, transform.scale);
if (transform.rotation) {
context.rotate(transform.rotation);
}
context.translate(transform.x, transform.y);
return context;
}
function getFarContext2d(element, transform) {
return far(element, transform).getContext("2d");
}
const referenceCanvas = document.getElementById("reference");
const farCanvas = document.getElementById("far");
const image = { data: document.createElement("img"), width: 320, height: 164 };
const canvasDimensions = { width: 700, height: 1200 };
referenceCanvas.width = canvasDimensions.width;
referenceCanvas.height = canvasDimensions.height;
farCanvas.width = canvasDimensions.width;
farCanvas.height = canvasDimensions.height;
const scale = canvasDimensions.width / image.width;
const focus = 10000; // 500000000 // breaks down in vanilla canvas
const diff = -image.height * 0;
const mkImage = ({ x, y, image }) => ({
x,
y,
data: image.data,
width: image.width,
height: image.height,
});
const images = [
mkImage({ x: 0, y: focus - 1 * image.height, image }),
mkImage({ x: 0, y: focus + 0 * image.height, image }),
mkImage({ x: 0, y: focus + 1 * image.height, image }),
mkImage({ x: 0, y: focus + 2 * image.height, image }),
mkImage({ x: 0, y: focus + 3 * image.height, image }),
mkImage({ x: 0, y: focus + 4 * image.height, image }),
];
const rectangles = [
{ x: 10, y: focus + 20, width: 200, height: 30 },
{ x: 100, y: focus + 250, width: 200, height: 30 },
{ x: -10, y: focus - 10, width: 200, height: 30 },
{ x: 100, y: focus + 400, width: 200, height: 30 },
{
x: 0,
y: focus + 2 * image.height,
width: image.width,
height: image.height,
},
];
const contextReference = getReferenceContext2d(
document.getElementById("reference"),
{ x: 0, y: -focus - diff, scale: scale }
);
const contextFar = getFarContext2d(document.getElementById("far"), {
x: 0,
y: -focus - diff,
scale: scale,
});
image.data.onload = function () {
function render(ctx) {
// Clear the canvas first
ctx.save();
ctx.resetTransform();
ctx.clearRect(0, 0, canvasDimensions.width, canvasDimensions.height);
ctx.restore();
images.forEach((image, i) => {
ctx.save();
if (i == 1) {
ctx.drawImage(image.data, image.x, image.y);
} else {
ctx.drawImage(
image.data,
image.x,
image.y,
image.width - i * 32,
image.height
);
}
ctx.beginPath();
ctx.strokeStyle = "#803";
ctx.lineWidth = 1;
ctx.rect(image.x, image.y, image.width, image.height);
ctx.stroke();
ctx.restore();
});
rectangles.forEach((rectangle) => {
ctx.save();
ctx.save();
ctx.fillStyle = "#CE0";
ctx.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
ctx.restore();
ctx.save();
ctx.strokeStyle = "#803";
ctx.lineWidth = 8;
ctx.beginPath();
ctx.moveTo(rectangle.x, rectangle.y);
ctx.lineTo(rectangle.x + rectangle.width, rectangle.y + rectangle.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(rectangle.x + rectangle.width, rectangle.y);
ctx.lineTo(rectangle.x, rectangle.y + rectangle.height);
ctx.stroke();
ctx.restore();
ctx.save();
ctx.fillStyle = "#F08";
ctx.fillText("example", rectangle.x, rectangle.y + 10);
ctx.font = "bold 48px serif";
ctx.strokeStyle = "#0F8";
ctx.strokeText("far", rectangle.x, rectangle.y + 48);
ctx.restore();
ctx.restore();
});
// Draw transform tests last (at the top of canvas)
testTransforms(ctx);
}
// Run rendering
render(contextReference);
render(contextFar);
};
image.data.src =
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Saturn_from_Cassini_Orbiter_%282004-10-06%29.jpg/320px-Saturn_from_Cassini_Orbiter_%282004-10-06%29.jpg";
// Test transform operations
function testTransforms(ctx) {
// Save current state before switching to screen space
ctx.save();
ctx.resetTransform();
// Clear the test area
ctx.fillStyle = "#EEE";
ctx.fillRect(0, 0, 400, 200);
// Draw a grid for reference
ctx.strokeStyle = "#CCC";
for (let x = 0; x <= 400; x += 50) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, 200);
ctx.stroke();
}
for (let y = 0; y <= 200; y += 50) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(400, y);
ctx.stroke();
}
// Draw labels
ctx.fillStyle = "#000";
ctx.font = "12px sans-serif";
ctx.fillText("1. Blue: translate(50,50)", 10, 15);
ctx.fillText("2. Green: scale(2,2)", 10, 30);
ctx.fillText("3. Red: rotate(45°)", 10, 45);
ctx.fillText("4. Yellow: setTransform(skew)", 10, 60);
// Test 1: Basic translation
ctx.save();
ctx.translate(50, 50);
ctx.fillStyle = "#00F";
ctx.fillRect(0, 0, 30, 30);
ctx.restore();
// Test 2: Scale
ctx.save();
ctx.translate(100, 50);
ctx.scale(2, 2);
ctx.fillStyle = "#0F0";
ctx.fillRect(-15, -15, 15, 15);
ctx.restore();
// Test 3: Rotation
ctx.save();
ctx.translate(150, 50);
ctx.rotate(Math.PI / 4); // 45 degrees
ctx.fillStyle = "#F00";
ctx.fillRect(-10, -10, 20, 20);
ctx.restore();
// Test 4: setTransform (absolute)
ctx.save();
ctx.setTransform(1, 0.2, 0.2, 1, 250, 75);
ctx.fillStyle = "#FF0";
ctx.fillRect(0, 0, 30, 30);
ctx.restore();
// Restore original state (back to world space)
ctx.restore();
}