-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrorschach.js
118 lines (99 loc) · 3.22 KB
/
rorschach.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
const canvas = document.getElementById('canvas');
canvas.width = 2000;
canvas.height = 1500;
canvas.style.width = "1000px";
canvas.style.height = "745px";
const visibleContext = canvas.getContext('2d');
visibleContext.scale(2,2);
const offscreenCanvas = document.createElement("canvas");
offscreenCanvas.width = canvas.width;
offscreenCanvas.height = canvas.height;
offscreenCanvas.style.width = canvas.style.width;
offscreenCanvas.style.height = canvas.style.height;
const offscreenContext = offscreenCanvas.getContext("2d");
const defaultCenterPoint = {
x: canvas.width * 0.25,
y: canvas.height * 0.25,
};
const defaultRadius = 100;
function randomPoint() {
let p = {
x: Math.random() * canvas.width * 0.25,
y: Math.random() * canvas.height * 0.25,
}
return p;
};
function skew() {
let s = Math.random() * 1;
s *= Math.floor(Math.random() * 2) == 1 ? 1 : -1;
return s * 0.5;
};
const randomRadius = () => Math.floor(Math.random() * (141)) + 10;
const blobX = (cx, radius, angle) => cx + (radius * Math.cos(angle));
const blobY = (cy, radius, angle) => cy + (radius * Math.sin(angle));
function drawBlob(centerPoint, radius, i, context){
const maxOffset = radius * 0.5;
const ctx = context;
const s = skew();
const c = {
x: centerPoint.x + radius,
y: centerPoint.y + radius
};
ctx.globalCompositeOperation = 'multiply';
ctx.beginPath();
ctx.moveTo(c.x, c.y);
let lastPoint = c;
for (let j = 0; j < 4; j++) {
for (let angle = -Math.PI/10; angle <= 2 * Math.PI; angle+= Math.PI/10) {
let r = radius + skew() * 2 * maxOffset;
let newPoint = {
x: blobX(c.x, r, angle + s) > canvas.width * 0.25 ? canvas.width * 0.25 : blobX(c.x, r, angle + s),
y: blobY(c.y, r, angle),
};
ctx.arcTo(
lastPoint.x,
lastPoint.y,
newPoint.x,
newPoint.y,
Math.random() * 10 + Math.sin(angle) + 1
);
lastPoint = newPoint;
}
ctx.arcTo(
lastPoint.x,
lastPoint.y,
c.x,
c.y,
10
);
ctx.closePath();
ctx.shadowColor = 'rgba('
+ Math.floor(Math.random() * 50 + 200)
+', '+ Math.floor(Math.random() * 60)
+', '+ Math.floor(Math.random() * 20)
+', 0.9)';
ctx.fillStyle = 'rgba('
+ Math.floor(Math.random() * 50 + 200)
+', '+ Math.floor(Math.random() * 60)
+', '+ Math.floor(Math.random() * 20)
+', 0.2)';
ctx.shadowBlur = Math.random() * 100;
ctx.fill();
};
};
function drawSplatter(numberOfBlobs, context) {
for(i = 0; i < numberOfBlobs; i++) {
drawBlob(randomPoint(), randomRadius(), i, context);
}
};
function draw() {
visibleContext.save();
drawSplatter(4, offscreenContext);
visibleContext.translate(canvas.width * 0.25, 0);
visibleContext.drawImage(offscreenCanvas, -canvas.width * 0.25, 0);
visibleContext.scale(-1, 1);
visibleContext.translate(-canvas.width * 0.25, 0);
visibleContext.drawImage(offscreenCanvas, 0, 0);
visibleContext.restore();
}
draw();