-
Notifications
You must be signed in to change notification settings - Fork 1
/
boxview.js
55 lines (46 loc) · 1.41 KB
/
boxview.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
"use strict";
define([], function() {
function create( params ) {
var canvas = document.createElement( 'canvas' );
canvas.width = params.width;
canvas.height = params.height;
var PTM = params.pixelsPerMeter;
var context = canvas.getContext( '2d' );
var origin = {
x:Math.floor( canvas.width/2 ),
y:Math.floor( canvas.height/2 )
}
function pixelToWorld( pixel ) {
return {
x: ( pixel.x - origin.x ) / PTM,
y: ( canvas.height - pixel.y - origin.y ) / PTM
}
}
function worldToPixel( world ) {
return {
x: world.x * PTM + origin.x,
y: -world.y * PTM + origin.y,
}
}
function metersToPixels( meters ) {
return meters * PTM;
}
function clear() {
context.fillStyle = 'rgb(0,0,0)';
context.fillRect( 0, 0, canvas.width, canvas.height );
}
return {
clear:clear,
canvas:canvas,
context:context,
getPTM: function() { return PTM; },
getOrigin: function() { return origin; },
pixelToWorld: pixelToWorld,
worldToPixel: worldToPixel,
metersToPixels:metersToPixels,
}
}
return {
create:create
}
});