-
Notifications
You must be signed in to change notification settings - Fork 2
/
svg.html
209 lines (183 loc) · 7.67 KB
/
svg.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
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>svg Demo</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<div id="drawing"></div>
<script>"use strict";
const input = {
scroll: 0,
cam_x: 0,
cam_y: 0,
cam_zoom: 1,
mouse_x: 0,
mouse_y: 0,
mouse_down_x: 0,
mouse_down_y: 0,
mouse_down: false,
mouse_released: false
};
let wheelTimeout;
window.onwheel = e => {
input.scroll = Math.sign(e.deltaY);
clearTimeout(wheelTimeout)
wheelTimeout = setTimeout(() => input.scroll = 0, 100)
};
window.onmousedown = e => {
input.mouse_down = true;
input.mouse_down_x = e.clientX;
input.mouse_down_y = e.clientY;
};
window.onmousemove = e => {
input.mouse_x = e.clientX;
input.mouse_y = e.clientY;
};
window.onmouseup = e => {
input.mouse_down = false;
input.mouse_released = true;
input.mouse_x = e.clientX;
input.mouse_y = e.clientY;
};
window.onload = () => {
const ns = 'http://www.w3.org/2000/svg';
const div = document.getElementById('drawing') ;
const svg = document.createElementNS(ns, 'svg');
div.appendChild(svg);
requestAnimationFrame(function frame() {
requestAnimationFrame(frame);
svg.setAttributeNS(null, 'width', '100vw');
svg.setAttributeNS(null, 'height', '100vh');
svg.children[0]?.remove();
svg.children[0]?.remove();
const group = document.createElementNS(ns, 'g');
const static_group = document.createElementNS(ns, 'g');
let cam_x = input.cam_x;
let cam_y = input.cam_y;
{
/* camera input handling */
{
if (input.mouse_down || input.mouse_released) {
cam_x += (input.mouse_x - input.mouse_down_x);
cam_y += (input.mouse_y - input.mouse_down_y);
}
if (input.mouse_released) {
input.mouse_released = false;
input.cam_x = cam_x;
input.cam_y = cam_y;
}
if (input.scroll) do {
break;
input.cam_zoom *= 1 + input.scroll*0.03;
const s = 1 / input.cam_zoom;
const min_x = cam_x*s;
const min_y = cam_y*s;
const max_x = (window.innerWidth + cam_x) * s;
const max_y = (window.innerHeight + cam_y) * s;
const focal_x = lerp(min_x, max_x, input.mouse_x / window.innerWidth );
const focal_y = lerp(min_y, max_y, input.mouse_y / window.innerHeight);
const zoom = 1.0 + 0.03*input.scroll;
const zoomed_min_x = (min_x - focal_x)*zoom + focal_x;
const zoomed_max_x = (max_x - focal_x)*zoom + focal_x;
const zoomed_min_y = (min_y - focal_y)*zoom + focal_y;
const zoomed_max_y = (max_y - focal_y)*zoom + focal_y;
const x = zoomed_min_x * input.cam_zoom + input.cam_x;
const y = zoomed_min_y * input.cam_zoom + input.cam_y;
const w = (zoomed_max_x - zoomed_min_x) * input.cam_zoom;
const h = (zoomed_max_y - zoomed_min_y) * input.cam_zoom;
input.cam_x = x;
input.cam_y = y;
input.cam_zoom = w / window.innerWidth;
} while(false);
}
let scale = input.cam_zoom;
const transform = `matrix(${scale} 0 0 ${scale} ${cam_x} ${cam_y})`;
group.setAttributeNS(null, 'transform', transform);
}
{
const rect = document.createElementNS(ns, 'rect');
rect.setAttributeNS(null, 'width', 100);
rect.setAttributeNS(null, 'height', 100);
rect.setAttributeNS(null, 'fill', '#f06');
group.appendChild(rect);
}
function corner(x, y) {
const rect = document.createElementNS(ns, 'rect');
rect.setAttributeNS(null, 'transform' , `translate(${x-5} ${y-5})`);
rect.setAttributeNS(null, 'width' , 10);
rect.setAttributeNS(null, 'height', 10);
rect.setAttributeNS(null, 'fill', '#60f');
group.appendChild(rect);
}
const s = input.cam_zoom;
const min_x = input.cam_x*s;
const min_y = input.cam_y*s;
const max_x = (window.innerWidth + input.cam_x) * s;
const max_y = (window.innerHeight + input.cam_y) * s;
const focal_x = lerp(min_x, max_x, input.mouse_x / window.innerWidth );
const focal_y = lerp(min_y, max_y, input.mouse_y / window.innerHeight);
{
const zoom = 1.0 + 0.03*input.scroll;
const zoomed_min_x = (min_x - focal_x)*zoom + focal_x;
const zoomed_max_x = (max_x - focal_x)*zoom + focal_x;
const zoomed_min_y = (min_y - focal_y)*zoom + focal_y;
const zoomed_max_y = (max_y - focal_y)*zoom + focal_y;
{
const rect = document.createElementNS(ns, 'rect');
rect.setAttributeNS(null, 'transform' , `translate(${min_x} ${min_y})`);
rect.setAttributeNS(null, 'width' , max_x - min_x);
rect.setAttributeNS(null, 'height', max_y - min_y);
rect.setAttributeNS(null, 'fill', 'none');
rect.setAttributeNS(null, 'stroke', '#6f0');
rect.setAttributeNS(null, 'stroke-width', '20');
group.appendChild(rect);
}
{
const rect = document.createElementNS(ns, 'rect');
const x = zoomed_min_x / input.cam_zoom - input.cam_x;
const y = zoomed_min_y / input.cam_zoom - input.cam_y;
const w = (zoomed_max_x - zoomed_min_x) / input.cam_zoom;
const h = (zoomed_max_y - zoomed_min_y) / input.cam_zoom;
const scale = w / window.innerWidth;
const transform = `matrix(${ 1 /scale} 0 0 ${ 1 / scale} ${-x} ${-y})`;
static_group.setAttributeNS(null, 'transform', transform);
input.cam_x += x;
input.cam_y += y;
input.cam_zoom *= scale;
if (0) {
{
const x = window.innerWidth * 0.5 - 50
const y = window.innerHeight * 0.5 - 50
rect.setAttributeNS(null, 'transform' , `translate(${x}, ${y})`);
}
rect.setAttributeNS(null, 'width' , 100);
rect.setAttributeNS(null, 'height', 100);
} else {
rect.setAttributeNS(null, 'transform', `translate(${x} ${y})`);
rect.setAttributeNS(null, 'width' , w);
rect.setAttributeNS(null, 'height', h);
}
rect.setAttributeNS(null, 'fill', 'none');
rect.setAttributeNS(null, 'stroke', '#f0f');
rect.setAttributeNS(null, 'stroke-width', '2');
static_group.appendChild(rect);
}
}
corner(focal_x, focal_y);
corner(min_x, min_y);
corner(max_x, max_y);
corner(max_x, min_y);
corner(min_x, max_y);
svg.appendChild( group);
svg.appendChild(static_group);
});
}
function lerp(v0, v1, t) { return (1 - t) * v0 + t * v1; }
function inv_lerp(min, max, p) { return (p - min) / (max - min); }
</script>
</body>
</html>