-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
147 lines (130 loc) · 3.85 KB
/
script.js
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
let imgDimensions = { w: 0, h: 0 };
let imgUrl,
canvas,
displayed,
pixel,
jost,
isPainting = false,
notStarted = true,
brushWeight = 30,
lastColor = [128, 128, 128, 20];
function preload() {
imgUrl =
defaultPaintings[Math.floor(Math.random() * defaultPaintings.length)];
displayed = loadImage(imgUrl);
jost = loadFont(
"https://cdn.glitch.com/78391c90-30ed-44d7-8ca3-ccd51ddd2e05%2FJost-VariableFont_wght.ttf?v=1594528844941"
);
}
function setup() {
getDimensions(imgUrl);
canvas = createCanvas(imgDimensions.w, imgDimensions.h);
canvas.parent("canvas-div");
background(235);
brushWeight = Math.floor(windowWidth / 15);
displayBrushSize();
noStroke();
}
function draw() {
if (isPainting && pmouseX != mouseX && pmouseY != mouseY) {
revealBrush();
} else if (notStarted) {
drawTouchPrompt();
}
}
//updates dimensions and returns a Promise after image finishes loading
function getDimensions(url) {
let image = new Image();
image.src = url;
return new Promise((resolve, reject) => {
image.onload = function() {
imgDimensions.w = this.width;
imgDimensions.h = this.height;
adjustCanvas();
reset();
resolve();
};
});
}
function reset(){
document.getElementById("error-display").innerHTML = "";
updateSourceLink();
notStarted = true;
}
function revealBrush() {
calculateColor();
let brushTemp = brushWeight;
let brushFrac = brushTemp / 20;
let speedTransform = calcSpeedTransform();
let rotation = Math.atan2(mouseY - pmouseY, mouseX - pmouseX);
let mainAxis, crossAxis;
translate(mouseX, mouseY);
rotate(rotation);
for (let i = 0; i < 15; i++) {
//layering for watercolor effect
mainAxis = Math.min(
Math.max(brushTemp * speedTransform[0], 5),
brushTemp * 2
);
crossAxis = Math.min(Math.max(brushTemp * speedTransform[1], 0), brushTemp);
ellipse(0, 0, mainAxis, crossAxis);
brushTemp -= brushFrac;
}
}
function calculateColor(){
displayed.resize(imgDimensions.w, imgDimensions.h);
pixel = displayed.get(mouseX, mouseY);
pixel = averageColor(pixel, lastColor);
pixel[3] = 12; //sets alpha to low value for watercolor effect
fill(pixel);
lastColor = pixel;
}
function calcSpeedTransform() {
let yval = Math.max(mouseY - pmouseY, 0.5); //ensure no division by 0
let xval = Math.max(mouseX - pmouseX, 0.5);
let speed = Math.min(Math.max(Math.sqrt(sq(yval) + sq(xval)), 1), 40); //restricted between [1,40]
if (speed >= 40) {
return [2, 0.5];
}
return [-10 / (speed - 40) + 1, Math.max(10 / (speed - 40) + 1, 1)];
}
function averageColor(color1, color2) {
let avgColor = [];
for (let i = 0; i < lastColor.length; i++) {
avgColor[i] = Math.sqrt((sq(color1[i]) + sq(color2[i])) / 2);
}
return avgColor;
}
function adjustCanvas() {
fitImage();
resizeCanvas(imgDimensions.w, imgDimensions.h);
background(235);
document.getElementById("canvas-div").width = imgDimensions.w + "px";
document.getElementById("canvas-div").height = imgDimensions.h - 100 + "px";
}
function fitImage() {
let minPicDim = Math.min(imgDimensions.w, imgDimensions.h);
let minWindowDim = Math.min(windowWidth, windowHeight);
if (minPicDim == imgDimensions.w) {
imgDimensions.w =
((minWindowDim * 2) / 3) * (imgDimensions.w / imgDimensions.h);
imgDimensions.h = (minWindowDim * 2) / 3;
} else if (minPicDim == imgDimensions.h) {
imgDimensions.h =
(minWindowDim - 100) * (imgDimensions.h / imgDimensions.w);
imgDimensions.w = minWindowDim - 100;
}
}
//prevents black lines when mouse moves off canvas
document
.getElementById("canvas-div")
.addEventListener("mouseenter", function() {
isPainting = true;
if (notStarted) {
notStarted = false;
background(235);
}
});
document.getElementById("canvas-div").addEventListener("mouseout", function() {
isPainting = false;
});