-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeconstruction.html
151 lines (113 loc) · 4.44 KB
/
deconstruction.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jason Pollock Deconstruction</title>
<style>
body {
margin: 0px;
padding: 0px;
}
#canvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
// Create a variable for the canvas and contect
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
// Add a function to keep the cavas 100% width and height
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
context.clearRect(0, 0, canvas.width, canvas.height);
}
window.addEventListener('resize', resizeCanvas, false);
resizeCanvas();
// Create vairables for any data that needs to be tracked
let x = null;
let y = null;
let previousX = null;
let previousY = null;
let width = null;
let colour = '#000000';
// Add the drawing code on mouse move
canvas.addEventListener('mousemove', function(event){
// Save the position from last mouse move
previousX = x;
previousY = y;
// Get the new position of the mouse
x = event.clientX;
y = event.clientY;
// If this is the second mouse move
if(previousX != null)
{
// Calculate the horizontal move distance
let distanceX = Math.round(x - previousX);
if(distanceX < 0) distanceX *= -1;
// Calcualte the vertical move distance
let distanceY = Math.round(y - previousY);
if(distanceY < 0) distanceY *= -1;
// Calcualte the diagonal distance
let distance = Math.hypot(distanceX,distanceY);
if(distance > 50) distance = 50;
// Start a new path
context.beginPath()
// Calculate the width based on the distance moved
let targetWidth = Math.round((50 - distance) / 3);
if(targetWidth < 3) targetWidth = 3;
// Instead of settig the width, we ease it
if(width == null) width = targetWidth;
else if(targetWidth < width) width --;
else if(targetWidth > width) width ++;
// Set the colour and width
context.strokeStyle = colour;
context.lineWidth = width;
// Draw the line
context.moveTo(previousX,previousY);
context.lineTo(x, y);
context.stroke();
// If the mosue if movin slow, draw a large circle
if(distance < 5)
{
context.fillStyle = colour;
context.beginPath();
context.arc(x, y, 50 - distance + Math.random() * 40, 0, 2 * Math.PI);
context.fill();
}
// If the mosue is moving fast, draw a circle the size of the stroke
// This prevents the line from looking lke a bunch of rectangles
else
{
context.fillStyle = colour;
context.beginPath();
context.arc(x, y, width / 2, 0, 2 * Math.PI);
context.fill();
}
// Add some random spatter
if(Math.random() > 0.8)
{
context.fillStyle = colour;
context.beginPath();
context.arc(
x - 20 + Math.random() * 40,
y - 20 + Math.random() * 40,
2 + Math.random() * 4, 0, 2 * Math.PI);
context.fill();
}
}
});
// This function changes the colour to a random colour when
// the canvas is clicked
canvas.addEventListener('click', function(){
colour = '#' + Math.floor(Math.random()*16777215).toString(16).padStart(6, '0').toUpperCase();
});
</script>
</body>
</html>