-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm-06.html
125 lines (112 loc) · 2.9 KB
/
vm-06.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
<html>
<head>
<title>06: Vera Molnar</title>
<style>
body {
width: 100vw;
height: 100vh;
font-family: Helvetica, Arial, sans-serif;
background-color: white;
color: black;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
canvas {
margin: 0px;
padding: 0px;
width: 90vw;
height: 90vh;
}
</style>
</head>
<body>
<canvas></canvas>
</body>
<script>
// Find a place we can draw:
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
// Variables:
var gridSize = 1;
var marginScale = 0.9;
var numColumns = 10;
function setgridSize () {
if (canvas.width > canvas.height) {
gridSize = canvas.height / numColumns;
}
else {
gridSize = canvas.width / numColumns;
}
}
function adjustMargins () {
var size = gridSize * numColumns;
var extraSpaceX = canvas.width - size;
var extraSpaceY = canvas.height - size;
context.translate(extraSpaceX/2, extraSpaceY/2);
setgridSize()
}
function resize () {
var {width: w, height: h} = canvas.getBoundingClientRect();
canvas.width = w;
canvas.height = h;
setgridSize();
draw();
}
window.addEventListener("resize", resize);
function drawSquare () {
var halfGrid = gridSize / 2;
// Expect the origin, 0,0 to be in the center.
var startX = - halfGrid;
var startY = - halfGrid;
var endX = halfGrid;
var endY = halfGrid;
context.beginPath();
context.moveTo(startX, startY);
context.lineTo(endX, startY);
context.lineTo(endX, endY) ;
context.lineTo(startX, endY) ;
context.closePath();
context.stroke();
}
function veraSquare() {
var i = 0;
var numSquares = 10;
var stepSize = 1 / numSquares;
// sale from 0 to 1 in numSquares steps
for (i = 0; i <= 1; i += stepSize) {
context.save();
context.scale(i, i);
drawSquare();
context.restore();
}
}
// Align drawing to our grid
function drawInGrid (gridX, gridY, drawingFunction) {
var halfGrid = gridSize / 2;
context.save();
context.translate(gridX * gridSize + halfGrid, gridY * gridSize + halfGrid);
context.scale(marginScale, marginScale);
drawingFunction();
context.restore();
}
// draw hella squares:
function draw () {
setgridSize();
adjustMargins();
context.save();
var x, y;
for(x=0; x < numColumns; x++) {
for(y=0; y < numColumns; y++) {
drawInGrid(x, y, veraSquare);
}
}
context.restore();
}
resize();
</script>
</html>