-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcanvastest.js
88 lines (79 loc) · 2.7 KB
/
canvastest.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
define(["./browsercanvas","ts!gfx/gfx"], function(browsercanvas, gfx) {
/*
if (window.navigator.userAgent.indexOf('iPhone') != -1) {
if (!window.navigator.standalone == true) {
// XXX display message prompting to add this app to the home screen
}
}
*/
var world;
var setup = function(canvas, uiEvents) {
world = gfx.WorldView.New();
world.canvas = canvas;
world.strokeColor = gfx.Color.black;
world.fillColor = gfx.Color.white;
// test content
var star = gfx.Polygon.newStar(5, 15, 30).shapedView();
star.strokeColor = gfx.Color.blue;
star.fillColor = gfx.Color.red;
star.strokeWidth = 4;
star = star.transformView();
star.translateBy(gfx.Point.New(100,100));
world.addFirst(star);
// Simplest possible widget; fill entire bounding box.
var BB = {
__proto__: gfx.ComposableView,
bounds: function() {
return gfx.Rectangle.New(gfx.Point.zero,
gfx.Point.New(30,30));
},
drawOn: function(canvas, clipRect) {
// ignore cliprect, just draw!
canvas.setFill(gfx.Color.grey);
canvas.beginPath();
canvas.rect(0,0,30,30);
canvas.fill();
}
};
var bb = BB.New();
bb = bb.transformView();
bb.translateBy(gfx.Point.New(50,50));
bb.touchStartEvent = function(event) {
event.handler.beginDragging(bb, event);
};
// Hello, world text.
var str = "_abcdefghijklmnopqrstuvwxyz0123456789,'ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var hello = gfx.TextView.New(world, str);
hello.fontHeight = 20;
hello.strokeColor = gfx.Color.black;
hello = hello.transformView();
hello.translateBy(gfx.Point.New(100,100));
world.addLast(hello);
world.addLast(bb);
// size feedback
var sizeWidget = gfx.TextView.New(world, "x");
sizeWidget.fillColor = null; // transparent
sizeWidget.strokeColor = gfx.Color.black;
world.addFirst(sizeWidget.transformView().translateBy(gfx.Point.New(5,5)));
// allow dragging the star
star.touchStartEvent = function(event) {
event.handler.beginDragging(star, event);
};
world.resizeEvent = function(event) {
canvas.resize(event.width, event.height, event.devicePixelRatio);
sizeWidget.text = event.width+"x"+event.height+" "+event.orientation;
world.damaged();
};
uiEvents.mapE(function(e) {
console.assert(e.name, e);
world.dispatchEvent(e);
});
// XXX used to put a widget at every touch to demonstrate multitouch
};
var drawFrame = function() {
if (!world.damage) return;
world.forceDamageToScreen();
};
// start it up!
browsercanvas.initEventLoop('canvas', setup, drawFrame);
});