-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-sketch.html
125 lines (115 loc) · 3.92 KB
/
canvas-sketch.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
<dom-module id="canvas-sketch">
<template>
<style>
:host {
display: block;
}
</style>
<div>
<canvas width="650" height="500" id="myCanvas"></canvas>
</div>
</template>
<script>
var x = 325;
var y = 250;
var canvas;
var context;
// looks to see if a direction is in a command and calls draw on it.
function drawDirection(command) {
console.log("command: " + command);
var sketch = document.querySelector('canvas-sketch');
if ( command.indexOf("West") > -1 || command.indexOf("west") > -1) {
console.log(command);
sketch.draw("West");
} else if (command.indexOf("East") > -1 || command.indexOf("east") > -1) {
console.log(command);
sketch.draw("East");
} else if (command.indexOf("North") > -1 || command.indexOf("north") > -1) {
console.log(command);
sketch.draw("North");
} else if (command.indexOf("South") > -1 || command.indexOf("south") > -1) {
console.log(command);
sketch.draw("South");
} else if (command.indexOf("Stop") > -1 || command.indexOf("stop") > -1) {
console.log(command);
sketch.draw("Stop");
} else if (command.indexOf("Clear") > -1 || command.indexOf("clear") > -1) {
console.log(command);
sketch.draw("Clear");
} else { sketch.draw("Draw"); }
};
function printCommand(command) {
var span = document.createElement("span");
var text = document.createTextNode(command + " ");
span.appendChild(text);
document.getElementById("drawCommands").appendChild(span);
};
function wipe() {
context.clearRect(0, 0, canvas.width, canvas.height);
x = 325;
y = 250;
};
Polymer({
is: 'canvas-sketch',
doDraw: false,
self: this,
// Interval drawing
drawing: 0,
draw: function(direction) {
// 'draw' just draws at the current position (as if to begin drawing)
// otherwise the proper position is changed and the canvas is drawn in the new location
switch(direction) {
case 'Draw':
clearInterval(self.drawing);
self.drawing = setInterval(function() { context.fillRect(x, y, 5, 5); }, 30);
console.log(drawing);
printCommand(direction);
break;
case 'North':
clearInterval(self.drawing);
self.drawing = setInterval(function() { context.fillRect(x, --y, 5, 5); }, 30);
console.log(drawing);
printCommand(direction);
break;
case 'South':
clearInterval(self.drawing);
self.drawing = setInterval(function() { context.fillRect(x, ++y, 5, 5); }, 30);
console.log(drawing);
printCommand(direction);
break;
case 'East':
clearInterval(self.drawing);
self.drawing = setInterval(function() { context.fillRect(++x, y, 5, 5); }, 30);
console.log(drawing);
printCommand(direction);
break;
case 'West':
clearInterval(self.drawing);
self.drawing = setInterval(function() { context.fillRect(--x, y, 5, 5); }, 30);
console.log(drawing);
printCommand(direction);
break;
case 'Stop':
console.log("Stopping " + self.drawing);
clearInterval(self.drawing);
console.log("Stopped.");
printCommand(direction);
break;
case 'Clear': {
console.log("Clearing board");
clearInterval(self.drawing);
wipe();
console.log("Cleared");
printCommand(direction);
break;
}
}
},
ready: function() {
canvas = this.$.myCanvas;
context = canvas.getContext('2d');
context.fillStyle = 'black';
}
});
</script>
</dom-module>