-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw-squares.js
132 lines (111 loc) · 4.4 KB
/
draw-squares.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
import {colors} from "./constants.js";
import divide from "./divide.js";
export default function drawSquares(canvas, arithmetic, properties) {
const top = 300;
const squareSize = 15;
const squareSpace = 20;
switch (arithmetic.operation) {
case "+": {
const left = properties.center - 9 * squareSpace;
canvas.fillStyle = colors.left;
for (let i = 0; i < arithmetic.operands.left; ++i) {
canvas.fillRect(left + i * squareSpace, top, squareSize, squareSize);
}
canvas.fillStyle = colors.right;
for (let i = 0; i < arithmetic.operands.right; ++i) {
canvas.fillRect(left + (arithmetic.operands.left * squareSpace) + i * squareSpace, top, squareSize, squareSize);
}
break;
}
case "−": {
const left = properties.center + Math.floor((squareSpace - squareSize) / 2);
const leftSquares = arithmetic.operands.left - arithmetic.operands.right;
const rightSquares = (leftSquares > 0) ? arithmetic.operands.right : arithmetic.operands.right + leftSquares;
if (leftSquares > 0) {
canvas.fillStyle = colors.left;
for (let i = 0; i < leftSquares; ++i) {
canvas.fillRect(left + i * squareSpace, top, squareSize, squareSize);
}
} else {
canvas.fillStyle = colors.right;
for (let i = 0; i < Math.abs(leftSquares); ++i) {
canvas.fillRect(left - (i + 1) * squareSpace, top, squareSize, squareSize);
}
}
canvas.fillStyle = colors.leftBlank;
for (let i = 0; i < rightSquares; ++i) {
canvas.fillRect(left + (Math.max(0, leftSquares) * squareSpace) + i * squareSpace, top, squareSize, squareSize);
}
break;
}
case "×": {
if (arithmetic.getResult() === 0) return;
const left = properties.center - Math.floor((9 * squareSpace) / 2);
// Left squares
canvas.fillStyle = colors.left;
for (let i = 0; i < arithmetic.operands.left; ++i) {
canvas.fillRect(left + i * squareSpace, top, squareSize, squareSize);
}
// Shared square
canvas.moveTo(left, top);
canvas.lineTo(left, top + squareSize);
canvas.lineTo(left + squareSize, top + squareSize);
canvas.moveTo(left, top);
canvas.fillStyle = colors.right;
canvas.fill();
// Right squares
for (let i = 1; i < arithmetic.operands.right; ++i) {
canvas.fillRect(left, top + i * squareSpace, squareSize, squareSize);
}
// Combined squares
canvas.beginPath();
for (let i = 1; i < arithmetic.operands.left; ++i) {
for (let j = 1; j < arithmetic.operands.right; ++j) {
canvas.rect(left + i * squareSpace, top + j * squareSpace, squareSize, squareSize);
}
}
const gradient = canvas.createLinearGradient(
left + arithmetic.operands.left * squareSpace,
top + squareSpace,
left + squareSpace,
top + arithmetic.operands.right * squareSpace,
);
gradient.addColorStop(0, colors.left);
gradient.addColorStop(1, colors.right);
canvas.fillStyle = gradient;
canvas.fill();
break;
}
case "÷": {
if (arithmetic.operands.left === 0 || arithmetic.operands.right === 0) return;
const left = properties.center - Math.floor(squareSpace * arithmetic.operands.left / 2);
const result = divide(arithmetic.operands.left, arithmetic.operands.right);
let leftSquares;
let drawFraction;
if (typeof result !== "object") {
leftSquares = result;
} else {
leftSquares = result.integer;
drawFraction = true;
}
canvas.fillStyle = colors.left;
for (let i = 0; i < leftSquares; ++i) {
canvas.fillRect(left + i * squareSpace, top, squareSize, squareSize);
}
if (arithmetic.operands.right > 1) {
const rightSquares = arithmetic.operands.left - leftSquares;
canvas.fillStyle = colors.leftBlank;
for (let i = 0; i < rightSquares; ++i) {
canvas.fillRect(left + (leftSquares) * squareSpace + i * squareSpace, top, squareSize, squareSize);
}
}
// Draw fraction square on top of leftmost right-square
if (drawFraction) {
const numeratorSquareSize = ((result.numerator / result.denominator) * squareSize);
canvas.fillStyle = colors.left;
canvas.fillRect(left + leftSquares * squareSpace, top, numeratorSquareSize, squareSize);
}
break;
}
}
}