-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path16.html
185 lines (147 loc) · 4.64 KB
/
16.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
179
180
181
182
183
184
185
<html>
<!--
The shape and location of the triangle drawn when the key `0` is held
down is determined by [the JSON data provided](https://github.com/DGMD-E-15
/Processing-Tutorial-01/blob/master/16.html#L26-30).
-->
<head>
<script src='processing-1.4.1.js'></script>
<style>
canvas {
outline: none;
}
/* So that when we click, the canvas isn't outlined */
</style>
</head>
<body>
<canvas></canvas>
</body>
<script>
/* @pjs font="Arial.ttf"; */
var canvas = document.getElementsByTagName('canvas')[0];
var data = {};
function sketch(p) {
data = {
'1': [8, 4],
'2': [8, 10],
'3': [10, 7]
}
var gridSize;
var gridColor;
var activeCol;
var activeRow;
var pactiveCol;
var pactiveRow;
var gridW;
var gridH;
var txt;
function setup() {
p.size(400, 300);
p.ellipseMode(p.CENTER);
p.rectMode(p.CENTER);
p.println("width: " + p.width + ", height: " + p.height);
x = p.width / 2;
y = p.height / 2;
direction = 1;
a = 0;
gridSize = 25;
gridColor = p.color(0x0000FF, 80);
p.textFont(p.createFont("Arial", 32));
//p.noLoop();
}
function draw() {
p.fill(0, 15);
p.rect(p.width / 2, p.height / 2, p.width, p.height);
grid(gridSize, gridColor, activeRow, activeCol);
trace(activeCol * gridSize, activeRow * gridSize, pactiveCol * gridSize, pactiveRow * gridSize);
txtW = p.textWidth(txt);
p.fill(255);
p.text(txt, ((activeCol - 0.5) * gridSize), ((activeRow - 0.5) * gridSize));
//p.println(p.__frameRate);
if (p.__keyPressed && (p.keyCode == '0')) {
p.stroke(gridColor);
p.noFill();
p.strokeWeight(5);
p.triangle(data[1][0] * gridSize, data[1][1] * gridSize, data[2][0] * gridSize,
data[2][1] * gridSize, data[3][0] * gridSize, data[3][1] * gridSize);
p.strokeWeight(1);
}
}
p.mousePressed = function() {
p.mouseDragged();
gridColor = p.color(p.random(255), p.random(255), p.random(255), 128);
}
p.mouseReleased = function() {
p.mouseMoved();
}
p.mouseMoved = function() {
pactiveCol = activeCol;
pactiveRow = activeRow;
activeCol = p.floor(p.mouseX / gridSize) + 0.5;
activeRow = p.floor(p.mouseY / gridSize) + 0.5;
}
p.mouseDragged = function() {
pactiveCol = activeCol;
pactiveRow = activeRow;
activeCol = gridW - p.floor(p.mouseX / gridSize) - 0.5;
activeRow = gridH - p.floor(p.mouseY / gridSize) - 0.5;
}
p.keyPressed = function() {
pactiveCol = activeCol;
pactiveRow = activeRow;
switch (p.keyCode) {
case 37:
activeCol -= 1;
if (activeCol < 0.5) activeCol = 0.5;
break;
case 39:
activeCol += 1;
if (activeCol > gridW - 0.5) activeCol = gridW - 0.5;
break;
case 38:
activeRow -= 1;
if (activeRow < 0.5) activeRow = 0.5;
break;
case 40:
activeRow += 1;
if (activeRow > gridH - 0.5) activeRow = gridH - 0.5;
break;
default:
if ((p.keyCode > 64) && (p.keyCode < 123)) {
txt = String.fromCharCode(p.keyCode);
}
break;
}
}
function grid(gridSize, gridColor, activeRow, activeCol) {
gridW = p.width / gridSize;
gridH = p.height / gridSize;
for (var row = 0.5; row < gridH; row++) {
for (var col = 0.5; col < gridW; col++) {
var dotSize;
if (typeof(activeCol) == 'undefined' || typeof(activeRow) == 'undefined') {
dotSize = gridSize * 0.9;
} else {
dotSize = gridSize * p.dist(row, col, activeRow, activeCol) / p.max(gridW, gridH);
}
p.noStroke();
p.fill(0, 64);
p.rect(col * gridSize, row * gridSize, gridSize, gridSize);
p.fill(gridColor);
p.ellipse(col * gridSize, row * gridSize, dotSize, dotSize);
}
}
}
function trace(x1, y1, x2, y2) {
p.fill(255);
p.stroke(255);
p.ellipse(x1, y1, 10, 10);
p.line(x1, y1, x2, y2);
p.ellipse(x2, y2, 10, 10);
}
p.setup = setup;
p.draw = draw;
}
var p = new Processing(canvas, sketch); // actually attach and run the sketch
</script>
</html>