-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrorDrawTool.js
143 lines (126 loc) · 4.74 KB
/
mirrorDrawTool.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
class mirrorDrawTool {
constructor() {
this.name = "mirrorDraw";
this.icon = "assets/mirrorDraw.jpg";
this.noHistory = true;
this.description = "mirrowDraw tool partition the screen into two and clone your drawing to the other side horizontally or vertically (depending on the mode selected). Click and drag on the canvas to use it"
//which axis is being mirrored (x or y) x is default
this.axis = "x";
//line of symmetry is halfway across the screen
this.lineOfSymmetry = width / 2;
//this changes in the p5.dom click handler. So storing it as
//a variable self now means we can still access this in the handler
var self = this;
//where was the mouse on the last time draw was called.
//set it to -1 to begin with
var previousMouseX = -1;
var previousMouseY = -1;
//mouse coordinates for the other side of the Line of symmetry.
var previousOppositeMouseX = -1;
var previousOppositeMouseY = -1;
this.draw = function () {
//display the last save state of pixels
updatePixels();
//do the drawing if the mouse is pressed
if (mouseIsPressed) {
//if the previous values are -1 set them to the current mouse location
//and mirrored positions
if (previousMouseX == -1) {
previousMouseX = mouseX;
previousMouseY = mouseY;
previousOppositeMouseX = this.calculateOpposite(mouseX, "x");
previousOppositeMouseY = this.calculateOpposite(mouseY, "y");
}
//if there are values in the previous locations
//draw a line between them and the current positions
else {
line(previousMouseX, previousMouseY, mouseX, mouseY);
previousMouseX = mouseX;
previousMouseY = mouseY;
//these are for the mirrored drawing the other side of the
//line of symmetry
var oX = this.calculateOpposite(mouseX, "x");
var oY = this.calculateOpposite(mouseY, "y");
line(previousOppositeMouseX, previousOppositeMouseY, oX, oY);
previousOppositeMouseX = oX;
previousOppositeMouseY = oY;
}
}
//if the mouse isn't pressed reset the previous values to -1
else {
previousMouseX = -1;
previousMouseY = -1;
previousOppositeMouseX = -1;
previousOppositeMouseY = -1;
}
//after the drawing is done save the pixel state. We don't want the
//line of symmetry to be part of our drawing
loadPixels();
//push the drawing state so that we can set the stroke weight and colour
push();
strokeWeight(3);
stroke("red");
//draw the line of symmetry
if (this.axis == "x") {
line(width / 2, 0, width / 2, height);
} else {
line(0, height / 2, width, height / 2);
}
//return to the original stroke
pop();
};
/*calculate an opposite coordinate the other side of the
*symmetry line.
*@param n number: location for either x or y coordinate
*@param a [x,y]: the axis of the coordinate (y or y)
*@return number: the opposite coordinate
*/
this.calculateOpposite = function (n, a) {
//if the axis isn't the one being mirrored return the same
//value
if (a != this.axis) {
return n;
}
//if n is less than the line of symmetry return a coorindate
//that is far greater than the line of symmetry by the distance from
//n to that line.
if (n < this.lineOfSymmetry) {
return this.lineOfSymmetry + (this.lineOfSymmetry - n);
}
//otherwise a coordinate that is smaller than the line of symmetry
//by the distance between it and n.
else {
return this.lineOfSymmetry - (n - this.lineOfSymmetry);
}
};
//when the tool is deselected update the pixels to just show the drawing and
//hide the line of symmetry. Also clear options
this.unselectTool = function () {
updatePixels();
//clear options
fill(c);
stroke(sc);
select(".options").html("");
};
//adds a button and click handler to the options area. When clicked
//toggle the line of symmetry between horizonatl to vertical
this.populateOptions = function () {
select(".options").html(
"<button id='directionButton'>Make Horizontal</button>", true
);
// //click handler
select("#directionButton").mouseClicked(function () {
var button = select("#" + this.elt.id);
if (self.axis == "x") {
self.axis = "y";
self.lineOfSymmetry = height / 2;
button.html("Make Vertical");
} else {
self.axis = "x";
self.lineOfSymmetry = width / 2;
button.html("Make Horizontal");
}
});
};
}
}