forked from rezoner/CanvasQuery
/
framework.dart
66 lines (64 loc) · 1.97 KB
/
framework.dart
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
part of examples;
var colors = ['red', 'blue', 'yellow', 'green', 'black', 'white'];
void framework(DivElement parent) {
var defaultWidth = 500;
var defaultHeight = 300;
var draw = false;
var currentColor = 0;
Point pos;
var buffer = cq(defaultWidth, defaultHeight);
var display = cq(defaultWidth, defaultHeight);
display..canvas.style.backgroundColor = 'white'
..appendTo(parent);
var framework = display.framework;
framework.onMouseMove.listen((position) => pos = position);
framework.onRender.listen((_) {
if (null != pos) {
if (draw) {
buffer..circle(pos.x, pos.y, 15)
..fillStyle = colors[currentColor]
..fill();
}
display..clear(color: 'white')
..globalAlpha = 1
..drawImage(buffer.canvas, 0, 0)
..globalAlpha = 0.2
..circle(pos.x, pos.y, 15)
..fillStyle = colors[currentColor]
..fill();
}
});
framework.onMouseDown.listen((e) {
if (1 == e.which) {
draw = true;
}
});
framework.onMouseUp.listen((e) {
if (1 == e.which) {
draw = false;
}
});
framework.onKeyDown.listen((keyCode) {
if (keyCode == KeyCode.LEFT) {
currentColor = (currentColor - 1) % colors.length;
} else if (keyCode == KeyCode.RIGHT) {
currentColor = (currentColor + 1) % colors.length;
}
});
framework.onDropImage.listen((image) {
buffer.canvas..width = image.width
..height = image.height;
display.canvas..width = image.width
..height = image.height;
buffer.drawImage(image, 0, 0);
}).onError((error) => window.alert(error.error));
framework.onSwipe().listen((direction) {
if (direction == 'down' && colors[currentColor] == 'white') {
buffer.canvas..width = defaultWidth
..height = defaultHeight;
display.canvas..width = defaultWidth
..height = defaultHeight;
buffer.clear();
}
});
}