-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.js
129 lines (94 loc) · 3.83 KB
/
train.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
var WILL = {
backgroundColor: Module.color.WHITE,
color: Module.color.from(204, 204, 204),
init: function(width, height) {
this.initInkEngine(width, height);
this.initEvents();
},
initInkEngine: function(width, height) {
this.canvas = new Module.InkCanvas(width, height);
this.strokesLayer = this.canvas.createLayer();
this.clear();
this.brush = new Module.DirectBrush();
this.pathBuilder = new Module.SpeedPathBuilder();
this.pathBuilder.setNormalizationConfig(182, 3547);
this.pathBuilder.setPropertyConfig(Module.PropertyName.Width, 2.05, 34.53, 0.72, NaN, Module.PropertyFunction.Power, 1.19, false);
this.smoothener = new Module.MultiChannelSmoothener(this.pathBuilder.stride);
this.strokeRenderer = new Module.StrokeRenderer(this.canvas, this.strokesLayer);
this.strokeRenderer.configure({brush: this.brush, color: this.color});
},
initEvents: function() {
var self = this;
$(Module.canvas).on("mousedown", function(e) {self.beginStroke(e);});
$(Module.canvas).on("mousemove", function(e) {self.moveStroke(e);});
$(document).on("mouseup", function(e) {self.endStroke(e);});
Module.canvas.addEventListener("touchstart", function(e) {self.beginStroke(e);});
Module.canvas.addEventListener("touchmove", function(e) {self.moveStroke(e);});
document.addEventListener("touchend", function(e) {self.endStroke(e);});
document.ontouchmove = function(е) {
е.preventDefault();
}
},
beginStroke: function(e) {
if (["mousedown", "mouseup"].contains(e.type) && e.button != 0) return;
this.inputPhase = Module.InputPhase.Begin;
if (e.changedTouches) e = e.changedTouches[0];
this.buildPath({x: e.clientX, y: e.clientY});
this.strokeRenderer.draw(this.pathPart, false);
this.strokeRenderer.blendUpdatedArea();
},
moveStroke: function(e) {
if (!this.inputPhase) return;
var self = this;
if (e.changedTouches) e = e.changedTouches[0];
this.pointerPos = {x: e.clientX, y: e.clientY};
this.inputPhase = Module.InputPhase.Move;
if (this.intervalID) return;
var lastPointerPos = this.pointerPos;
this.drawPoint();
this.intervalID = setInterval(function() {
if (self.inputPhase && lastPointerPos != self.pointerPos) {
self.drawPoint();
lastPointerPos = self.pointerPos;
}
}, 16);
},
drawPoint: function() {
this.buildPath(this.pointerPos);
this.strokeRenderer.draw(this.pathPart, false);
this.strokeRenderer.blendUpdatedArea();
this.strokeRenderer.color = Module.color.RED;
this.strokeRenderer.drawPreliminary(this.preliminaryPathPart);
this.strokeRenderer.color = this.color;
},
endStroke: function(e) {
if (!this.inputPhase) return;
this.inputPhase = Module.InputPhase.End;
clearInterval(this.intervalID);
delete this.intervalID;
if (e.changedTouches) e = e.changedTouches[0];
this.buildPath({x: e.clientX, y: e.clientY});
this.strokeRenderer.draw(this.pathPart, true);
this.strokeRenderer.blendUpdatedArea();
this.canvas.blendWithMode(this.strokesLayer, Module.BlendMode.NONE); ///////////
delete this.inputPhase;
},
buildPath: function(pos) {
if (this.inputPhase == Module.InputPhase.Begin)
this.smoothener.reset();
var pathPart = this.pathBuilder.addPoint(this.inputPhase, pos, Date.now()/1000);
var smoothedPathPart = this.smoothener.smooth(pathPart, this.inputPhase == Module.InputPhase.End);
var pathContext = this.pathBuilder.addPathPart(smoothedPathPart);
this.pathPart = pathContext.getPathPart();
var preliminaryPathPart = this.pathBuilder.createPreliminaryPath();
var preliminarySmoothedPathPart = this.smoothener.smooth(preliminaryPathPart, true);
this.preliminaryPathPart = this.pathBuilder.finishPreliminaryPath(preliminarySmoothedPathPart);
},
clear: function() {
this.strokesLayer.clear(this.backgroundColor);
this.canvas.clear(this.backgroundColor);
}
};
Module.addPostScript(function() {
WILL.init(1600, 600);
});