Skip to content

Commit daaef44

Browse files
committed
Circles
1 parent 7535672 commit daaef44

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

chapter-19/circles.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8" />
3+
<script src="paint.js"></script>
4+
5+
<div></div>
6+
<script>
7+
function circle(center, state, dispatch) {
8+
function drawCircle(pos) {
9+
const radius = Math.floor(Math.sqrt((center.x - pos.x) ** 2 + (center.y - pos.y) ** 2));
10+
11+
let drawn = [];
12+
for (let y = 0; y < state.picture.height; y++) {
13+
for (let x = 0; x < state.picture.width; x++) {
14+
const distance = Math.sqrt((center.x - x) ** 2 + (center.y - y) ** 2);
15+
if (distance < radius) {
16+
drawn.push({ x, y, color: state.color });
17+
}
18+
}
19+
}
20+
dispatch({ picture: state.picture.draw(drawn) });
21+
}
22+
drawCircle(center);
23+
return drawCircle;
24+
}
25+
26+
let dom = startPixelEditor({
27+
tools: Object.assign({}, baseTools, { circle }),
28+
});
29+
document.querySelector('div').appendChild(dom);
30+
</script>

0 commit comments

Comments
 (0)