-
Notifications
You must be signed in to change notification settings - Fork 2
/
sketchmachine_variable_width_bezier_stroke.js
292 lines (222 loc) · 6.96 KB
/
sketchmachine_variable_width_bezier_stroke.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
var layer,layerStroke;
var layerNum=0;
var np;
var op;
var p1;
var pc;
var p2;
var stroke=[];
var brushQuill = {
min:1,
max:30,
acc:10,
limiter: 2000,
friction: 1.1
};
var dirtyQuill = {
min:4,
max:20,
acc:2,
drops:2000,
dropsFrecuency: 100,
dropsSmooth: 400,
limiter: 120,
friction: 0.2
};
var brushPen = {
min:0,
max:3,
acc:4,
limiter: 2000,
friction: 1.2
};
var brush = brushQuill;
var d=0;
var acc=0;
var drops=1; // Drops level
var flow=1; // Stroke flow (efecto de presion mediante la aceleración)
var strokes = [];
var draw=false;
var shiftKey=false;
var controlKey=false;
window.mouse={x:0,y:0};
ACCURATE=120;
ACCURATEcurr=0;
function keyPressed(event) {
if (event.shiftKey) shiftKey=true; else shiftKey=false;
if (event.ctrlKey) controlKey=true; else controlKey=false;
}
window.addEventListener('keydown',keyPressed,false);
window.addEventListener('keyup' ,keyPressed,false);
function init() {
stage = new Layer({ x:0, y:0, w:window.innerWidth, h:window.innerHeight, debug:false });
stage.appendTo( document.body );
layerStroke = new Layer({ id:"STROKE",x:0, y:0, w:window.innerWidth, h:window.innerHeight, debug:false });
layerStroke.appendTo( document.body );
layerStroke.domElm.style.opacity=1;
/*
let filterid="pen";
layerStroke.domElm.style.filter=`url(#${filterid})`;
stage.domElm.style.filter=`url(#${filterid})`;
*/
let tickFPS = new FPS('fps');
updateMouseEvent = function() {
window.mouse = new Vector( event.clientX - stage.x/stage.RETINA, event.clientY - stage.y/stage.RETINA);
if (draw) {
drawing();
}
tickFPS.update();
}
window.addEventListener("mousedown", startDraw);
window.addEventListener("mouseup", stopDraw);
window.addEventListener("mousemove", updateMouseEvent);
}
function startDraw(event) {
updateMouseEvent(event);
stroke = []; // 0
d=brush.min;
acc=brush.acc;
drawing();
draw=true;
}
function drawing(last) {
var newPos = new Vector( window.mouse.x, window.mouse.y );
if (!last) {
/*
// Controla el tiempo entre trazos para no crear más de los necesarios
var curTime=new Date().getTime();
if ( curTime < ACCURATEcurr+1000/ACCURATE ) {
return;
} else ACCURATEcurr=curTime;
*/
if ( stroke.last() && Math.distance(stroke.last().p, newPos)<3 ) {
return; // Don't draw if not movement
}
}
stroke.push({
p: newPos,
d: d
});// 1 ... n
if(stroke.length<3) {
return;
}
stroke.last().p = stroke.prior().p.lrp( stroke.last().p,.4); // Smooth positions
if (last) { // last Stroke
var p1 = stroke.in(-2).p.med( stroke.prior().p );
var pc = stroke.prior().p;
var p2 = stroke.last().p;
} else if (stroke.length==3) { // first stroke can be drawn
var p1 = stroke.in(-2).p;
var pc = stroke[1].p;
var p2 = stroke[1].p.med( stroke[2].p );
} else {
var p1 = stroke.in(-2).p.med( stroke.prior().p );
var pc = stroke.prior().p;
var p2 = stroke.last().p.med( stroke.prior().p );
}
var np = stroke.last().p;
// DIRTY TRICKS - To solve the union betwen each trace
p1=p1.sub( p1.vec(pc).uni().mul(.1) );
//p2=p2.add( pc.vec(p2).uni().mul(.1) );
var strokeDistance = Math.distance2(p1,pc)+Math.distance2(pc,p2);
acc += strokeDistance / brush.limiter;
acc = acc / brush.friction;
d = Math.lerp( brush.min, brush.max, 1/acc);
d = Math.lerp( stroke.prior().d, d, .1);
d = Math.clamp(d, brush.min, brush.max);
if(brush.drops) {
if (Math.random(0,100)<brush.dropsFrecuency) {
d = Math.lerp( d, Math.random(brush.min,brush.drops), 1/brush.dropsSmooth);
}
}
//dirty
// d= Math.random(brush.min, brush.max);
// d=Math.lerp( stroke.prior().d, d, .2);
stroke.last().d = d;
layerStroke.strokeStyle=undefined;
layerStroke.lineWidth=undefined;
layerStroke.fillStyle="black";
//layerStroke.globalAlpha=.2
var b = new QuadBezier(p1,pc,p2);
var t = b.closestTtoPc();
var pt = b.getPointAtT(t);
var strokeWeight = stroke.prior().d;
var p1n = b.normalUnitVectorAtT(0);
var p1L = p1.add( p1n.mul(+strokeWeight/2) );
var p1R = p1.add( p1n.mul(-strokeWeight/2) );
var strokeWeight = Math.lerp(stroke.prior().d, stroke.last().d, t);
var ptn = b.normalUnitVectorAtT(t);
var ptL = pt.add( ptn.mul(+strokeWeight/2) );
var ptR = pt.add( ptn.mul(-strokeWeight/2) );
var strokeWeight = stroke.last().d;
var p2n = b.normalUnitVectorAtT(1);
var p2L = p2.add( p2n.mul(+strokeWeight/2) );
var p2R = p2.add( p2n.mul(-strokeWeight/2) );
// Cap
if (last) {
layerStroke.beginPath();
let angStart= new Vector(1,0).ang(p2R.vec(p2L));
layerStroke.arc(p2.sub(pc.vec(p2).uni().mul(.1)), stroke.last().d/2 , angStart, angStart+Math.PI);
layerStroke.fill();
} else if (stroke.length==3) {
layerStroke.beginPath();
let angStart= new Vector(1,0).ang(p1L.vec(p1R));
layerStroke.arc(p1.add(p1.vec(pc).uni().mul(.1)), stroke.first().d/2 , angStart, angStart+Math.PI);
layerStroke.fill();
}
if (b.angle<Math.PI/4) { // IF the angle is closed
var p1c = b.getControlPointOfASegment(0,t);
var b1 = new QuadBezier(p1,p1c,pt);
var p1m = p1n.add(ptn);
var p1t = Math.lerp( 0,t, b1.closestTtoPc() ); // Interpolate over 0-t (original bezier b) the t of p1 p1c pt
var strokeWeight = Math.lerp(stroke.prior().d, stroke.last().d, p1t); // Use that t to know the weight on that point
var p1cL = p1c.add( p1m.mul( +strokeWeight/p1m.length2() ) ); // Expand with that strokeWeight the bezier stroke
var p1cR = p1c.add( p1m.mul( -strokeWeight/p1m.length2() ) );
var p2c = b.getControlPointOfASegment(t,1);
var b2 = new QuadBezier(pt,p2c,p2);
var p2m = ptn.add(p2n);
var p2t = Math.lerp( t,1, b2.closestTtoPc() );
var strokeWeight = Math.lerp(stroke.prior().d, stroke.last().d, p2t);
var p2cL = p2c.add( p2m.mul( +strokeWeight/p2m.length2() ) );
var p2cR = p2c.add( p2m.mul( -strokeWeight/p2m.length2() ) );
// DIRTY TRICKS - To solve the union betwen each trace
ptLfix=ptL.add( ptL.vec(ptR).per().uni().mul(.1) );
ptRfix=ptR.add( ptL.vec(ptR).per().uni().mul(.1) );
layerStroke.beginPath();
layerStroke.moveTo(p1L);
layerStroke.quadraticCurveTo(p1cL, ptLfix);
layerStroke.lineTo(ptRfix);
layerStroke.quadraticCurveTo(p1cR, p1R);
layerStroke.fill();
layerStroke.beginPath();
layerStroke.moveTo(p2L);
layerStroke.quadraticCurveTo(p2cL, ptL);
layerStroke.lineTo(ptR);
layerStroke.quadraticCurveTo(p2cR, p2R);
layerStroke.fill();
} else {
ptm = p1n.add(p2n);
var strokeWeight = Math.lerp(stroke.prior().d, stroke.last().d, t);
ptcL = pc.add( ptm.mul( +strokeWeight/ptm.length2() ) );
ptcR = pc.add( ptm.mul( -strokeWeight/ptm.length2() ) );
layerStroke.beginPath();
layerStroke.moveTo(p1L);
layerStroke.quadraticCurveTo(ptcL, p2L);
layerStroke.lineTo(p2R);
layerStroke.quadraticCurveTo(ptcR, p1R);
layerStroke.fill();
}
}
function stopDraw(event) {
updateMouseEvent(event);
draw=false;
d=brush.min;
acc=brush.acc;
drawing(true); //Last
stage.globalAlpha = layerStroke.domElm.style.opacity;
stage.copy(layerStroke);
stage.globalAlpha = 1;
stage.globalCompositeOperation = "source-over";
layerStroke.clear();
}
window.ready(init);