3 <title>Binary logic
</title>
4 <link rel=
"stylesheet" href=
"style/pixelless.css" />
5 <script type=
"text/javascript" src=
"script/canvas.js" ></script>
6 <style type=
"text/css">
9 -moz-box-shadow: 0 .2em .6em black
;
10 -moz-border-radius: .2em;
16 <div class=
"content header">
20 <canvas width=
"800px" height=
"500px">Canvas support required
</canvas>
22 <div id=
"empty_footer"></div>
25 © 2009 Vynogradov
28 <script type=
"text/javascript">
30 var tag
= document
.getElementsByTagName('canvas')[0];
31 var ctx
= tag
.getContext('2d');
33 ctx
.strokeStyle
= 'silver';
36 var objects
= [new Gate(20, 20), new Gate(20, 60)];
40 tag
.addEventListener('mousedown', mouseDown
, false);
41 tag
.addEventListener('mouseup', mouseUp
, false);
42 tag
.addEventListener('mousemove', mouseMove
, false);
46 var offset
= new Point(0,0);
50 if(down
&& dragging
!== null)
52 var point
= mouseToCanvas(e
);
53 dragging
.x
= point
.x
- offset
.x
;
54 dragging
.y
= point
.y
- offset
.y
;
61 var point
= mouseToCanvas(e
);
62 dragging
= findUnder(point
);
64 offset
= new Point(point
.x
- dragging
.x
, point
.y
- dragging
.y
);
71 offset
= new Point(0,0);
82 var under
= objects
.filter(function(o
){return o
.x
< p
.x
&& o
.y
< p
.y
&& o
.x
+ w
> p
.x
&& o
.y
+ h
> p
.y
});
83 return under
.length
> 0 ? under
[0] : null;
87 function mouseToCanvas(e
)
97 else // we assume DOM modeled javascript
103 while (elt
.offsetParent
)
105 left
+= elt
.offsetLeft
;
106 top
+= elt
.offsetTop
;
107 elt
= elt
.offsetParent
;
114 return new Point(x
, y
);
123 renderGate = function(gate
)
125 // TODO: Render distinctive shape for each type of gate.
127 .strokeRect(gate
.x
, gate
.y
, w
, h
)
129 .moveTo(gate
.x
+ w
, gate
.y
+ h
/2)
130 .lineTo(gate
.x
+ w
* 1.3, gate
.y
+ h
/2)
133 .moveTo(gate
.x
, gate
.y
+ h
/ 5)
134 .lineTo(gate
.x
- w
/3, gate.y + h / 5)
137 .moveTo(gate
.x
, gate
.y
+ 4 * h
/ 5)
138 .lineTo(gate
.x
- w
/3, gate.y + 4 * h / 5)
145 document
.getElementsByTagName('h1')[0].innerHTML
= mouse
.x
+ ', ' + mouse
.y
;
146 ctx
.clearRect(0,0,800, 500);
147 objects
.forEach(renderGate
)