-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
96 lines (84 loc) · 2.36 KB
/
index.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
<!doctype html>
<html>
<body>
<style>
#canvas-container {
width: 100%;
text-align:center;
}
#myCanvas{
margin:0 auto;
display: inline;
}
</style>
<div id="canvas-container">
<canvas id="myCanvas" width="600" height="400"></canvas><br>
<button onclick="zoom(10)">+</button>
<button onclick="zoom(-10)">-</button>
</div>
<script>
let canvas = document.getElementById('myCanvas')
let ctx = canvas.getContext('2d')
let scaleFactor = 200
let panX = 1
let panY = 1
let requestId
function paint(canvas, ctx, scaleFactor, panX, panY){
for(let x = 0; x < canvas.width; x++){
for(let y = 0; y < canvas.height; y++){
let color = checkIfBelongsToMandelbrotSet(x/scaleFactor - panX,
y/scaleFactor - panY)
ctx.fillStyle = color === 0 ? '#000' : `hsl(0, 100%, ${color}%)`
ctx.fillRect(x,y, 1,1)
}
}
}
function checkIfBelongsToMandelbrotSet(x, y){
let oX = x, oY = y
let maxIterations = 100
for(let i=0; i < maxIterations; i++){
let tempX = x * x - y * y - oX
let tempY = 2 * x * y - oY
x = tempX
y = tempY
if (x * y > 4)
return (i * 255 /maxIterations)
}
return 0
}
let paintFrame = () =>{
if(requestId){
console.log("clearing earlier paint request")
cancelAnimationFrame(requestId)
}
requestId = requestAnimationFrame(() => {
paint(canvas, ctx, scaleFactor, panX, panY)
})
}
let timeout
let zoom = factor => {
scaleFactor += factor
if(timeout){
clearTimeout(timeout)
}
timeout = setTimeout(paintFrame, 200)
}
let isDown = false; // whether mouse is pressed
let startCoords = []; // 'grab' coordinates when pressing mouse
canvas.onmousedown = function(e) {
isDown = true;
startCoords = [
e.offsetX, // set start coordinates
e.offsetY
];
};
canvas.onmouseup = function(e) {
isDown = false;
panX += (e.offsetX - startCoords[0]) / 100
panY += (e.offsetY - startCoords[1]) / 100
paintFrame();
};
paintFrame()
</script>
</body>
</html>