-
Notifications
You must be signed in to change notification settings - Fork 2
/
circles.html
34 lines (31 loc) · 966 Bytes
/
circles.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
<!doctype html>
<script src="./code/19_paint.js"></script>
<div></div>
<script>
function circle(pos, state, dispatch) {
function drawCircle(to) {
let radius = Math.sqrt( Math.pow(to.x - pos.x, 2) +
Math.pow(to.y - pos.y, 2));
let radiusC = Math.ceil(radius);
let drawn = [];
for (let dy = -radiusC; dy <= radiusC; dy++) {
for (let dx = -radiusC; dx <= radiusC; dx++) {
let dist = Math.sqrt( Math.pow(dx,2) +
Math.pow(dy,2));
if (dist > radius) continue;
let y = pos.y + dy, x = pos.x + dx;
if (y < 0 || y > state.picture.height ||
x < 0 || x > state.picture.width) continue;
drawn.push({x, y, color: state.color});
}
}
dispatch({picture: state.picture.draw(drawn)});
}
drawCircle(pos);
return drawCircle;
}
let dom = startPixelEditor({
tools: Object.assign({}, baseTools, {circle})
});
document.querySelector("div").appendChild(dom);
</script>