-
Notifications
You must be signed in to change notification settings - Fork 3
/
bezier.html
178 lines (148 loc) · 4.68 KB
/
bezier.html
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
<html><head>
<title>Bezier example</title>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"><script type="text/javascript">
var canvas=null;
var ctx=null;
var p0_x=0;
var p0_y=0;
var p1_x=0;
var p1_y=0;
var p2_x=100;
var p2_y=100;
var p3_x=100;
var p3_y=100;
var threshold=27;
var x0_offset=25;
var y0_offset=125;
var x_max=100;
var y_max=100;
var selected=1;
var scale=2;
function init() {
// get the canvas element using the DOM
canvas = document.getElementById('pressurecurve');
// Make sure we don't execute when canvas isn't supported
if (canvas.getContext) {
drawCurve();
} else {
alert('You need Safari or Firefox 1.5+ to see this demo.');
}
}
function drawCurve() {
// use getContext to use the canvas for drawing
ctx = canvas.getContext('2d');
// Clear old canvas
canvas.width = 1;
canvas.width = (x0_offset+x_max+100)*scale;
canvas.height = (y0_offset+50)*scale;
ctx.scale(scale,scale);
// use getContext to use the canvas for drawing
ctx = canvas.getContext('2d');
// Draw graph axis
ctx.beginPath();
ctx.moveTo(x0_offset,y0_offset-y_max);
ctx.lineTo(x0_offset,y0_offset);
ctx.lineTo(x0_offset+x_max,y0_offset);
ctx.strokeStyle = "rgb(0,0,0)";
ctx.stroke();
// Draw Bezier Curve
ctx.beginPath();
ctx.moveTo(x0_offset-p0_x,y0_offset-p0_y);
ctx.bezierCurveTo(x0_offset+p1_x,y0_offset-p1_y,
x0_offset+p2_x,y0_offset-p2_y,
x0_offset+p3_x,y0_offset-p3_y);
ctx.strokeStyle = "rgb(0,0,0)";
ctx.stroke();
// Draw reference line from 1st to 2nd control points
ctx.beginPath();
ctx.moveTo(x0_offset-p0_x,y0_offset-p0_y);
ctx.strokeStyle = "rgb(127,127,127)";
ctx.lineTo(x0_offset+p1_x,y0_offset-p1_y);
ctx.stroke();
// Draw reference line from 3rd to 4th control points
ctx.beginPath();
ctx.moveTo(x0_offset+p2_x,y0_offset-p2_y);
ctx.strokeStyle = "rgb(127,127,127)";
ctx.lineTo(x0_offset+p3_x,y0_offset-p3_y);
ctx.stroke();
// Draw fixed first and fourth control points
ctx.fillStyle = "rgb(0,0,255)";
ctx.fillRect(x0_offset+p0_x,y0_offset-p0_y, 4, 4);
str="("+(p0_x)+","+(p0_y)+")";
ctx.fillText(str, x0_offset+p0_x+10,
y0_offset-p0_y+10);
ctx.fillStyle = "rgb(0,0,255)";
ctx.fillRect(x0_offset+p3_x,y0_offset-p3_y, 4, 4);
str="("+(p3_x)+","+(p3_y)+")";
ctx.fillText(str, x0_offset+p3_x+10,
y0_offset-p3_y+10);
// Draw second and third control points
ctx.fillStyle = "rgb(255,0,0)";
ctx.fillRect(x0_offset+p1_x, y0_offset-p1_y, 4, 4);
str="("+(p1_x)+","+(p1_y)+")";
ctx.fillText(str, x0_offset+p1_x+10,
y0_offset-p1_y+10);
ctx.fillStyle = "rgb(255,0,255)";
ctx.fillRect(x0_offset+p2_x, y0_offset-p2_y, 4, 4);
str="("+(p2_x)+","+(p2_y)+")";
ctx.fillText(str, x0_offset+p2_x+10,
y0_offset-p2_y+10);
// Shade out Threshold range
ctx.fillStyle = "rgba(127,127,127,0.5)";
ctx.fillRect(x0_offset, y0_offset-y_max,
Math.round(threshold/(2048/100)), y_max);
ctx.fillStyle = "rgb(0,255,0)";
ctx.fillRect(x0_offset+Math.round(threshold/(2048/100)),
y0_offset-y_max, 4, 4);
str="("+threshold+"/(2048/100))";
ctx.fillText(str, x0_offset+Math.round(threshold/(2048/100))+10,
y0_offset-y_max);
}
function moveHandler(event) {
var x=Math.round(((event.clientX-canvas.offsetLeft)/scale)-x0_offset);
var y=Math.round(y0_offset-((event.clientY-canvas.offsetTop)/scale));
if (selected == 0) {
// This point is fixed to 0,0 in the driver
//p0_x=x;
//p0_y=y;
} else if (selected == 1) {
p1_x=x;
p1_y=y;
} else if (selected == 2) {
p2_x=x;
p2_y=y;
} else if (selected == 3) {
// This point is fixed to 100,100 in the driver
//p3_x=x;
//p3_y=y;
}
drawCurve();
}
function downHandler(event) {
selected=99;
var x=Math.round(((event.clientX-canvas.offsetLeft)/scale)-x0_offset);
var y=Math.round(y0_offset-((event.clientY-canvas.offsetTop)/scale));
if (x >= p1_x-1 && x <= p1_x+4 && y >= p1_y-4 && y <= p1_y+1) {
selected = 1;
} else if (x >= p2_x-1 && x <= p2_x+4 && y >= p2_y-4 && y <= p2_y+1) {
selected = 2;
} else if (x >= p0_x-1 && x <= p0_x+4 && y >= p0_y-4 && y <= p0_y+1) {
selected = 0;
} else if (x >= p3_x-1 && x <= p3_x+4 && y >= p3_y-4 && y <= p3_y+1) {
selected = 3;
}
if (selected != 99) {
canvas.onmousemove=moveHandler;
}
}
function upHandler(event) {
canvas.onmousemove=null;
}
</script>
</head><body onload="init();">
<h2>Wacom Pressure Curve and Threshold</h2>
<div>
<canvas id="pressurecurve" onmousedown="downHandler(event);" onmouseup="upHandler(event);" width="450" height="350"></canvas>
</div>
<pre>Red=p1,Purple=p2,Green=Threshold</pre>
</body></html>