-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
143 lines (114 loc) · 4.26 KB
/
index.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
// < begin copyright >
// Copyright Ryan Marcus 2017
//
// This file is part of humanLines.
//
// humanLines is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// humanLines is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with humanLines. If not, see <http://www.gnu.org/licenses/>.
//
// < end copyright >
const blur = require('glur');
const seedrandom = require("seedrandom");
module.exports.generatePencilTexture = generatePencilTexture;
function generatePencilTexture() {
// Meraj et al. didn't publish their co-occr matrix, so we can't
// exactly replicate what they did. Instead, we'll just use smoothed
// noise.
const width = 300;
const height = 300;
const buffer = new Uint8ClampedArray(width*height*4);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const val = Math.random() * 120;
const pos = (y * width + x)*4;
buffer[pos] = val;
buffer[pos+1] = val;
buffer[pos+2] = val;
buffer[pos+3] = 255; // alpha channel
}
}
blur(buffer, width, 2);
const root = (global && global.document
? global.document : document);
const canvas = root.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
const idata = ctx.createImageData(width, height);
idata.data.set(buffer);
ctx.putImageData(idata, 0, 0);
return canvas;
}
function timeToPoint(sx, sy, fx, fy, t) {
// scale the time value, which should be between 0 and 2, to 0 and 1
const tau = t / 2.0;
const polyTerm = 15 * Math.pow(tau, 4)
- 6 * Math.pow(tau, 5)
- 10 * Math.pow(tau, 3);
return {"x": sx + (sx - fx) * polyTerm,
"y": sy + (sy - fy) * polyTerm};
}
function getSquiggle(prev, next, seed=seed) {
// find the midpoint
const midpoint = {"x": (prev.x + next.x) / 2,
"y": (prev.y + next.y) / 2};
// displace by a random value between -5 and 5
// the paper calls to do this w.r.t. the normal of the line
// but we'll just do it on the circle.
let rng = Math.random;
if (seed) {
rng = seedrandom(seed);
}
const displacementX = rng() * 10 - 5;
const displacementY = rng() * 10 - 5;
midpoint.x += displacementX;
midpoint.y += displacementY;
return midpoint;
}
module.exports.drawLine = drawLine;
function drawLine(ctx, sx, sy, fx, fy, seed=false, drawPoints = false) {
const dist = Math.sqrt(Math.pow(sx - fx, 2) + Math.pow(sy - fy, 2));
let dt = false;
if (dist < 200) {
dt = 0.5;
} else if (dist < 400) {
dt = 0.3;
} else {
dt = 0.2;
}
let lastPoint = {"x": sx, "y": sy};
ctx.beginPath();
ctx.moveTo(lastPoint.x, lastPoint.y);
for (let t = 0; t <= 2.0; t += dt) {
const currentPoint = timeToPoint(sx, sy, fx, fy, t);
const squiggleControlPoint = getSquiggle(lastPoint, currentPoint, seed=seed);
ctx.quadraticCurveTo(squiggleControlPoint.x, squiggleControlPoint.y,
currentPoint.x, currentPoint.y);
if (drawPoints) {
ctx.fillStyle = "black";
ctx.fillRect(currentPoint.x - 4, currentPoint.y - 4, 8, 8);
ctx.fillStyle = "red";
ctx.fillRect(squiggleControlPoint.x - 4,
squiggleControlPoint.y - 4, 8, 8);
}
lastPoint = currentPoint;
}
ctx.stroke();
}
module.exports.drawRect = drawRect;
function drawRect(ctx, x, y, w, h, seed=false) {
drawLine(ctx, x, y , x + w, y , seed=seed);
drawLine(ctx, x + w, y , x + w, y + h, seed=seed);
drawLine(ctx, x + w, y + h, x , y + h, seed=seed);
drawLine(ctx, x , y + h, x , y , seed=seed);
}