@private
-->
this.draw = function() {
this.canvas.beginPath();
if(this.fillColor!==null) {
this.canvas.fillStyle = this.fillColor;
}
this.canvas.strokeStyle = this.color;
this.canvas.moveTo(this.radius, 0);
this.canvas.arcTo(this.width, 0, this.width, this.height, this.radius);
this.canvas.arcTo(this.width, this.height, 0, this.height, this.radius);
this.canvas.arcTo(0, this.height, 0, 0, this.radius);
this.canvas.arcTo(0, 0, this.width, 0, this.radius);
if(this.fillColor!==null) {
this.canvas.fill();
}
this.canvas.stroke();
};
};
Giraffe.RoundedRectangle.prototype = new Giraffe.GraphicsObject();
//Deprecated
RoundedRectangle = Giraffe.RoundedRectangle;
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.Arc = function(x,y,startAngle,sweepAngle,radius) {
this.x=x;
this.y=y;
this.closed=true;
this.startAngle=startAngle;
this.sweepAngle=sweepAngle;
this.radius=radius;
this.type="Arc";
this.draw = function()
{
this.canvas.beginPath();
if(this.fillColor!=null)
{
this.canvas.fillStyle = this.fillColor;
}
var startAngleRad = this.startAngle*2*Math.PI/360;
var sweepAngleRad = this.startAngle+this.sweepAngle)%360)*2*Math.PI/360;
this.canvas.strokeStyle = this.color;
if(this.closed==true) {
this.canvas.moveTo(0,0);
this.canvas.lineTo(
Math.cos(startAngleRad)*radius,
Math.sin(startAngleRad)*radius
);
}
this.canvas.arc(0,0,this.radius,startAngleRad,sweepAngleRad,false);
if(this.closed==true) {
this.canvas.lineTo( 0,0 );
}
this.canvas.closePath();
if(this.fillColor!=null)
{
this.canvas.fill();
}
this.canvas.stroke();
};
this.setColor = function(color) {
this.color = color;
return this;
};
this.setFillColor = function(fillColor) {
this.fillColor= fillColor;
return this;
};
this.isInside = function(posX,posY) {
var rPos = this.getRelativePosition(posX,posY);
posX = rPos.x;
posY = rPos.y;
var xl = posX;
var yl = posY;
var d = Math.pow( (xl*xl)+(yl*yl),0.5);
if( d0) a=Math.PI+a;
if(a<0) a=(Math.PI*2)+a;
a = (a*180)/(Math.PI);
if(a>=this.startAngle &&
a<=this.startAngle+this.sweepAngle) {
return true;
} else {
return false;
}
} else {
return false;
}
};
this.setClosed = function() {
this.closed=true;
return this;
};
this.setOpen = function() {
this.closed=false;
return this;
};
};
Giraffe.Arc.prototype = new Giraffe.GraphicsObject();
//Deprecated
Arc = Giraffe.Arc;
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.PixelCanvas = function(x,y,width,height) {
this.x=x;
this.y=y;
this.width=width;
this.height=height;
var imageData = new ImageData(width,height);
this.setPixel = function(x,y,r,g,b,a) {
var cursor = (x+(y*width
*4;
if(!g) { // Assume color object
var color = r;
r = color.red;
g = color.green;
b = color.blue;
a = color.alpha;
}
imageData.datacursor = r;
imageData.datacursor+1 = g;
imageData.datacursor+2 = b;
imageData.datacursor+3 = a;
};
this.getPixel = function(x,y) {
var cursor = (x+(y*width))*4;
return {red: imageData.datacursor, green: imageData.datacursor+1, blue: imageData.datacursor+2, alpha: imageData.datacursor+3};
};
this.draw = function() {
this.canvas.putImageData(imageData,x,y);
};
this.isInside = function(posX,posY) {
var rPos = this.getRelativePosition(posX,posY);
posX = rPos.x;
posY = rPos.y;
if(
posX>0 &&
posX<(this.img.width*this.scaleX) &&
posY>0 &&
posY<(this.img.height*this.scaleY)
){ return true; } else { return false; }
};
};
Giraffe.PixelCanvas.prototype = new Giraffe.GraphicsObject();
//Deprecated
PixelCanvas = Giraffe.PixelCanvas;
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.Composite = function(x,y,rotation) {
var self = this;
if(typeof rotation == "undefined") rotation = 0;
self.rotation = rotation;
self.x=x;
self.y=y;
self.parts =
;
self.masked = false;
self.type = "Composite";
self.excludeFromSave=false;
var superToJson = self.toJson;
self.toJson = function() {
var json = superToJson(self);
json.parts =
;
if(self.excludeFromSave == true) return json;
for(var p=0;pp.toJson() );
}
return json;
};
var superFromJson = self.fromJson;
self.fromJson = function(json,canvas) {
superFromJson( json, canvas, self );
if(json.parts) {
for(var p=0;pp,canvas) );
}
}
};
var setParent = function( composite, canvasParent ) {
if(!composite.parts) return;
composite.canvasParent = canvasParent;
for(var p in composite.parts) {
composite.partsp.canvasParent = canvasParent;
if( composite.partsp.parts ) {
setParent( composite.partsp, canvasParent );
}
}
};
self.add = function( part,id ) {
if(part.canvasParent!=null) {
return;
}
if(id) {
selfid = part;
part.id = id;
} else if(part.id) {
selfid = part.id;
}
if( self.canvasParent ) {
setParent( part, self.canvasParent );
}
part.parent = this;
self.parts.push(part);
return self;
};
self.remove = function( part ) {
for(var i=0;ii==part) {
part.canvasParent=null;
part.parent = null;
self.parts.splice(i,1);
}
}
};
self.deconstruct = function() {
for(var i=0;ii;
part.x += self.x;
part.y += self.y;
part.onMouseOver = self.onMouseOver;
part.onMouseOut = self.onMouseOut;
part.onMousePressed = self.onMousePressed;
part.onMouseReleased = self.onMouseReleased;
part.onClick = self.onClick;
part.rotation += self.rotation;
part.canvasParent=null;
part.parent = null;
self.canvasParent.add( part );
}
self.parts =
;
};
self.draw = function() {
var hasMask = false;
for(var i=0;ii.canvasParent==null) {
self.partsi.canvasParent=self.canvasParent;
self.partsi.canvas=self.canvas;
}
if(self.partsi.visible==true) {
self.partsi._repaint();
if(i==0 && self.masked==true) {
hasMask = true;
self.canvasParent._store();
self.canvas.clip();
}
}
}
if(hasMask) {
self.canvasParent._restore();
}
};
self.animate = function(frame) {
for(var i=0;ii.animate(frame);
}
};
self.isInside = function(posX,posY) {
var rPos = self.getRelativePosition(posX,posY);
posX = rPos.x;
posY = rPos.y;
for(var i=self.parts.length-1;i>=0;i--) {
if(self.partsi.visible===true && self.partsi.isInside(posX,posY)) {
return true;
}
}
return false;
};
self.onClick = function(posX,posY) {
var relativePos = self.getRelativePosition(posX,posY);
for(var i=0;ii.visible===true && self.partsi.isInside(relativePos.x,relativePos.y)) {
self.partsi.onClick(relativePos.x,relativePos.y);
}
}
return false;
};
self.onMouseOver = function(posX,posY) {
var relativePos = self.getRelativePosition(posX,posY);
for(var i=0;ii.visible===true && self.partsi.isInside(relativePos.x,relativePos.y)) {
self.partsi.onMouseOver(relativePos.x,relativePos.y);
self.partsi.mouseOver=true;
} else if(self.partsi.mouseOver==true) {
self.partsi.mouseOver=false;
self.partsi.onMouseOut(relativePos.x,relativePos.y);
}
}
};
self.onMouseOut = function(posX,posY) {
var relativePos = self.getRelativePosition(posX,posY);
for(var i=0;ii.mouseOver==true) {
self.partsi.mouseOver=false;
self.partsi.onMouseOut(relativePos.x,relativePos.y);
}
}
};
self.onMousePressed = function(posX,posY) {
var relativePos = self.getRelativePosition(posX,posY);
for(var i=0;ii.visible===true && self.partsi.isInside(relativePos.x,relativePos.y)) {
self.partsi.onMousePressed(relativePos.x,relativePos.y);
}
}
};
self.onMouseReleased = function(posX,posY) {
var relativePos = self.getRelativePosition(posX,posY);
for(var i=0;ii.onMouseReleased(relativePos.x,relativePos.y);
}
};
};
Giraffe.Composite.prototype = new Giraffe.GraphicsObject();
//Deprecated
Composite = Giraffe.Composite;
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.RadialColor = function(canvas,color1,color2,x,y,radius) {
this.canvas = canvas.canvasContext;
this.colorStop =
;
this.colorStop0 = 0,color1,0,0,radius;
this.colorStop1 = 1,color2,x,y,0;
this.getColor = function() {
gradient = this.canvas.createRadialGradient(
this.colorStop12,this.colorStop13,this.colorStop14,
this.colorStop02,this.colorStop03,this.colorStop04
);
var i=0;
for(i=0;ii0,this.colorStopi1);
}
return gradient;
};
this.toJson = function() {
return {color1: color1, color2: color2, x: x, y: y, radius: radius};
};
this.fromJson = function( json ) {
var color1 = json.color1;
var color2 = json.color2;
var x = json.x;
var y = json.y;
var radius = json.radius;
this.colorStop0 = 0,color1,0,0,radius;
this.colorStop1 = 1,color2,x,y,0;
};
};
//Deprecated
RadialColor = Giraffe.RadialColor;
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
function GradientColor(canvas,color1,color2,x1,y1,x2,y2) {
this.canvas = canvas.canvasContext;
this.colorStop =
;
this.colorStop0 = 0,color1,x1,y1;
this.colorStop1 = 1,color2,x2,y2;
this.getColor = function() {
gradient = this.canvas.createLinearGradient(
this.colorStop12,this.colorStop13,
this.colorStop02,this.colorStop03
);
var i=0;
for(i=0;ii0,this.colorStopi1);
}
return gradient;
};
this.toJson = function() {
return {color1: color1, x1: x1, y1: y1, color2: color2, x2: x2, y2: y2};
};
this.fromJson = function( json ) {
var color1 = json.color1;
var color2 = json.color2;
var x1 = json.x1;
var y1 = json.y1;
var x2 = json.x2;
var y2 = json.y2;
this.colorStop =
;
this.colorStop0 = 0,color1,x1,y1;
this.colorStop1 = 1,color2,x2,y2;
};
}
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.Shadow = function() {
this.color = '#999';
this.blur = 20;
this.offsetX = 15;
this.offsetY = 15;
this.toJson = function() {
return {color: this.color, blur: this.blur, offsetX: this.offsetX, offsetY: this.offsetY};
};
this.fromJson = function( json ) {
this.color = json.color;
this.blur = json.blur;
this.offsetX = json.offsetX;
this.offsetY = json.offsetY;
};
};
//Deprecated
Shadow = Giraffe.Shadow;
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.LineStyle = function() {
this.thickness=1.5;
this.endCap = 'round';
this.type = "LineStyle";
this.setThickness = function(thickness) {
this.thickness = thickness;
return this;
};
this.setEndCap = function(endCap) {
this.endCap = endCap;
return this;
};
this.toJson = function() {
return {thickness: this.thickness, endCap: this.endCap, type: this.type};
};
this.fromJson = function( json ) {
this.thickness = json.thickness;
this.endCap = json.endCap;
this.type = json.type;
};
};
//Deprecated
LineStyle = Giraffe.LineStyle;
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.setAnimated = function(canvas) {
canvas.frame = 0;
canvas.timer = null;
canvas.frames = 0;
canvas.looped = true;
canvas.fps = 0;
canvas.animationListeners = new Array();
canvas.animated = false;
canvas.addAnimationListener = function(listener) {
this.animationListenersthis.animationListeners.length=listener;
};
canvas.removeAnimationListener = function(listener) {
for(this.loop=0;this.loopthis.loop==listener) {
this.animationListeners.splice(this.loop,1);
}
}
};
canvas.startAnimation = function(fps,frames,looped)
{
this.animated = true;
this.frame = 0;
this.frames = frames;
this.looped = looped;
this.fps = fps;
this.timer = setTimeout("Giraffe.canvases""target="editor" style="color: red" title="The page /OpenForum/Editor?pageName=/OpenForum/Giraffe/Core/
""+this.id+"
" does not exist. Click to create it.">
""+this.id+"
".animate();",1000/this.fps);
};
canvas.stopAnimation = function()
{
this.animated = false;
};
canvas.processing = false;
canvas.animate = function()
{
if(this.animated == false) return;
if(this.processing===true) {
console.log("running slow");
return;
}
this.processing = true;
try{
for(this.loop=0;this.loopthis.loop.processFrame(this.frame);
}
for(this.loop=0;this.loopthis.loop.animate(this.frame);
}
this.repaint();
this.frame++;
if(this.frame>=this.frames)
{
if(this.looped==true)
{
this.frame=0;
}
else
{
this.stopAnimation();
}
}
} catch(e) {
console.log(e);
}
this.processing = false;
this.timer = setTimeout("Giraffe.canvases""target="editor" style="color: red" title="The page /OpenForum/Editor?pageName=/OpenForum/Giraffe/Core/
""+this.id+"
" does not exist. Click to create it.">
""+this.id+"
".animate();",1000/this.fps);
};
};
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.Transition = function(target,frames) {
this.frames = frames;
this.target = target;
this.canvas = null;
this.currentFrame=0;
this.play = false;
this.start = function() {
this.register();
this.currentFrame=0;
this.play=true;
};
this.process = function(frame){};
this.processFrame = function() {
if(this.play==false) { return; };
this.currentFrame++;
if(this.currentFrame==this.frames+1) {
this.unregister();
this.doNext();
}
if(this.currentFrame>this.frames) {
return false;
}
this.process(this.currentFrame);
};
this.doNext = function(){};
this.register = function(){
this.canvas.addAnimationListener(this);
};
this.unregister = function(){
this.canvas.removeAnimationListener(this);
};
};
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.FlipOutX = function(target,frames) {
this.frames = frames;
this.target = target;
this.canvas = target.canvasParent;
this.register();
this.currentFrame=0;
this.process = function(frame) {
this.target.scaleX = 1-1*frame)/this.frames);
};
};
Giraffe.FlipOutX.prototype = new Giraffe.Transition();
Giraffe.FlipInX = function(target,frames) {
this.frames = frames;
this.target = target;
this.canvas = target.canvasParent;
this.register();
this.currentFrame=0;
this.process = function(frame) {
this.target.scaleX = (1*frame)/this.frames;
};
};
Giraffe.FlipInX.prototype = new Giraffe.Transition();
Giraffe.FlipOutY = function(target,frames) {
this.frames = frames;
this.target = target;
this.canvas = target.canvasParent;
this.register();
this.currentFrame=0;
this.process = function(frame){
this.target.scaleY = 1-
*frame)/this.frames);
};
};
Giraffe.FlipOutY.prototype = new Giraffe.Transition();
Giraffe.FlipInY = function(target,frames) {
this.frames = frames;
this.target = target;
this.canvas = target.canvasParent;
this.register();
this.currentFrame=0;
this.process = function(frame) {
this.target.scaleY = (1*frame)/this.frames;
};
};
Giraffe.FlipInY.prototype = new Giraffe.Transition();
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.MoveSequence = function(target,frames,matrix) {
this.frames = frames;
this.target = target;
this.matrix = matrix;
this.canvas = target.canvasParent;
this.register();
this.currentFrame=0;
this.process = function(frame) {
if(this.matrix) {
this.target.x+=this.matrixGiraffe.X;
this.target.y+=this.matrixGiraffe.Y;
} else {
this.target.x+=this.target.vx;
this.target.y+=this.target.vy;
}
};
};
Giraffe.MoveSequence.prototype = new Giraffe.Transition();
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.RotationSequence = function(target,frames,steps) {
this.frames = frames;
this.target = target;
this.steps = steps;
this.canvas = target.canvasParent;
this.register();
this.currentFrame=0;
this.process = function(frame) {
this.target.setRotation( this.target.rotation+(Giraffe.DEG_TO_RAD*this.steps) );
};
};
Giraffe.RotationSequence.prototype = new Giraffe.Transition();
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.ExplodeSequence = function(target,frames) {
this.frames = frames;
this.target = target;
this.canvas = target.canvasParent;
this.register();
this.currentFrame=0;
this.process = function(frame) {
if(!this.parts) {
this.parts = getExplodingParts(target);
}
var hasMinVelocity = false;
for(i=0;ii.x+=this.partsi.vx;
this.partsi.y+=this.partsi.vy;
this.partsi.vx*=0.9;
this.partsi.vy*=0.9;
if(this.partsi.vx>0.5 || this.partsi.vx<-0.5 ||
this.partsi.vy>0.5 || this.partsi.vy<-0.5) {
hasMinVelocity=true;
}
}
if(hasMinVelocity==false) {
this.currentFrame = this.frames;
}
};
var getExplodingParts = function(composite,parts) {
if(!parts) {
var parts =
;
}
for(var i=0;ii.parts) { //is sub composite
getExplodingParts(composite.partsi,parts);
}
composite.partsi.vx=composite.partsi.x+ath.random()*10)-5);
composite.partsi.vy=composite.partsi.y+ath.random()*10)-5);
partsparts.length=composite.partsi;
}
return parts;
};
};
Giraffe.ExplodeSequence.prototype = new Giraffe.Transition();
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.Interactive = {
shiftKeyDown : false,
controlKeyDown : false,
keyDown : null,
keyDownListeners :
,
keyUpListeners :
,
init : function() {
document.onkeydown = Giraffe.Interactive.keyDownHandler;
document.onkeyup = Giraffe.Interactive.keyUpHandler;
shiftKeyDown = false;
controlKeyDown = false;
},
keyDownHandler : function(e) {
var pressedKey;
if (document.all) { e = window.event;
pressedKey = e.keyCode; }
if (e.which) {
pressedKey = e.which;
}
if(pressedKey===16) {
Giraffe.Interactive.shiftKeyDown=true;
} else if(pressedKey===17) {
Giraffe.Interactive.controlKeyDown=true;
}
Giraffe.Interactive.keyDown = pressedKey;
for(var l in Giraffe.Interactive.keyDownListeners) {
Giraffe.Interactive.keyDownListenersl(pressedKey);
}
},
keyUpHandler : function(e) {
var pressedKey;
if (document.all) { e = window.event;
pressedKey = e.keyCode; }
if (e.which) {
pressedKey = e.which;
}
if(pressedKey===16) {
Giraffe.Interactive.shiftKeyDown=false;
} else if(pressedKey===17) {
Giraffe.Interactive.controlKeyDown=false;
}
for(var l in Giraffe.Interactive.keyUpListeners) {
Giraffe.Interactive.keyUpListenersl(pressedKey);
}
Giraffe.Interactive.keyDown = null;
},
isDragging : function() {
if( self.dragging && self.dragging.length>0 ) {
return true;
} else {
return false;
}
},
setInteractive : function(canvas) {
var canvas = canvas;
canvas.convertEvent = function(event,element) {
position = getPosition(element);
x=event.x-positionGiraffe.X+window.scrollX;
y=event.y-positionGiraffe.Y+window.scrollY;
x = x / canvas.scaleX;
y = y / canvas.scaleY;
return {
x : x, y : y
};
};
canvas.onClick = function(event) {
event = canvas.convertEvent(event,canvas.canvasElement);
for(var loop=canvas.graphicsObjects.length-1;loop>=0;loop--)
{
if(canvas.graphicsObjectsloop.visible===true && canvas.graphicsObjectsloop.isInside(event.x,event.y {
canvas.graphicsObjectsloop.onClick(event.x,event.y);
}
}
};
canvas.onMouseDown = function(event) {
event = canvas.convertEvent(event,canvas.canvasElement);
if(canvas.dragAndDrop==true) {
canvas.dragStart=event.x,event.y;
canvas.lastDx = 0;
canvas.lastDy = 0;
canvas.dragging=new Array();
}
for(var d = canvas.draggable.length-1; d>=0 ; d--) {
dragTarget=canvas.draggabled;
var root = dragTarget;
var stack =
;
while( root.parent ) {
root = root.parent;
stack.push(root);
}
var x = event.x;
var y = event.y;
for( var i = stack.length-1; i>=0; i-- ) {
var p = stacki.getRelativePosition( x,y );
x = p.x;
y = p.y;
}
if(dragTarget.isInside( x, y )) {
var included = false;
for(var check in canvas.dragging) {
check = canvas.draggingcheck;
if(check==dragTarget) {
included=true;
break;
}
}
if(included==true) {
continue;
}
dragTarget.dragging=true;
canvas.draggingcanvas.dragging.length=dragTarget;
dragTarget.dragStart=dragTarget.x,dragTarget.y;
if(Giraffe.Interactive.shiftKeyDown==false) break;
}
}
for(this.loop=0;this.loopthis.loop.visible===true && canvas.graphicsObjectsthis.loop.isInside(event.x,event.y)) {
canvas.graphicsObjectsthis.loop.onMousePressed(event.x,event.y);
}
}
canvas.onMousePressed(event.x,event.y);
};
canvas.onMousePressed = function(x,y) {};
canvas.onMouseReleased = function(x,y) {};
canvas.setDragging = function(dragTarget) {
// alert("Here: "+dragTarget.x+","+dragTarget.y);
dragTarget.dragging=true;
canvas.draggingcanvas.dragging.length=dragTarget;
dragTarget.dragStart=dragTarget.x,dragTarget.y;
};
canvas.onMouseUp = function(event) {
event = canvas.convertEvent(event,canvas.canvasElement);
if(canvas.dragAndDrop==true && canvas.dragging.length>0) {
for(this.loop=0;this.loopthis.loop.dragging) {
canvas.graphicsObjectsthis.loop.dragging=false;
}
if(canvas.graphicsObjectsthis.loop.isInside(event.x,event.y)) {
if(canvas.graphicsObjectsthis.loop.onCatch) {
for(var dropped in canvas.dragging) {
dropped = canvas.draggingdropped;
if(canvas.graphicsObjectsthis.loop==dropped) {
continue;
}
canvas.graphicsObjectsthis.loop.onCatch(dropped,event.x,event.y);
}
}
}
}
canvas.dragging=new Array();
}
for(this.loop=0;this.loopthis.loop.onMouseReleased(event.x,event.y);
}
canvas.onMouseReleased(event.x,event.y);
};
canvas.onMouseMoved = function(event) {
event = canvas.convertEvent(event,canvas.canvasElement);
if(canvas.dragAndDrop==true) {
var dragDX = event.x-canvas.dragStart0;
var dragDY = event.y-canvas.dragStart1;
for(var dragTarget in canvas.dragging) {
dragTarget = canvas.draggingdragTarget;
var scaleX = 1;
var scaleY = 1;
var parent = dragTarget.parent;
while( parent ) {
scaleX = scaleX * parent.scaleX;
scaleY = scaleY * parent.scaleY;
parent = parent.parent;
}
dragTarget.x = dragTarget.x+dragDX-canvas.lastDx)/scaleY);
dragTarget.y = dragTarget.y+
ragDY-canvas.lastDy)/scaleX);
}
canvas.lastDx = dragDX;
canvas.lastDy = dragDY;
}
for(this.loop=0;this.loopthis.loop.isInside(event.x,event.y {
canvas.graphicsObjectsthis.loop.mouseOver=true;
canvas.graphicsObjectsthis.loop.onMouseOver(event.x,event.y);
} else if(canvas.graphicsObjectsthis.loop.mouseOver==true) {
canvas.graphicsObjectsthis.loop.mouseOver=false;
canvas.graphicsObjectsthis.loop.onMouseOut(event.x,event.y);
}
}
canvas.onMouseOver(event.x,event.y);
};
canvas.onMouseOver = function(x,y){};
canvas.makeDraggable = function(object) {
this.dragAndDrop = true;
this.draggablethis.draggable.length=object;
object.draggable = true;
};
canvas.removeDraggable = function(object) {
//this.dragAndDrop = false;
var foundIndex = -1;
for(var index in this.draggable) {
if(this.draggableindex==object) {
foundIndex = index;
break;
}
}
if(foundIndex!=-1) {
object.draggable = false;
this.draggable.splice(foundIndex,1);
}
};
canvas.dragStart = 0,0;
canvas.dragging = new Array();
canvas.draggable = new Array();
canvas.dragAndDrop = false;
canvas.canvasElement.onmousemove = canvas.onMouseMoved;
canvas.canvasElement.onmouseup = canvas.onMouseUp;
canvas.canvasElement.onmousedown = canvas.onMouseDown;
canvas.canvasElement.onclick = canvas.onClick;
}
};
Giraffe.Interactive.init();
function setReveal(target,reveal) {
target.onMouseOver = function(x,y) {
reveal.visible=true;
};
target.onMouseOut = function(x,y) {
reveal.visible=false;
};
reveal.visible=false;
}
function getPosition(obj) {
this.curleft = this.curtop = 0;
if (obj.offsetParent) {
this.curleft = obj.offsetLeft;
this.curtop = obj.offsetTop;
obj = obj.offsetParent;
while (obj) {
this.curleft += obj.offsetLeft;
this.curtop += obj.offsetTop;
obj = obj.offsetParent;
}
}
return this.curleft,this.curtop;
}
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.Tablet = {
enableTouch : function(canvas) {
var canvas = canvas;
canvas.canvasElement.addEventListener("touchstart",
function(event) {
event.preventDefault();
canvas.onMouseDown( {x:event.targetTouches0.pageX,y:event.targetTouches0.pageY } );
}, false
);
canvas.canvasElement.addEventListener("touchmove",
function(event) {
event.preventDefault();
canvas.onMouseMoved( {x:event.targetTouches0.pageX,y:event.targetTouches0.pageY } );
}, false
);
canvas.canvasElement.addEventListener("touchend",
function(event) {
event.preventDefault();
canvas.onMouseUp( {x:0,y:0} );
}, false
);
}
};
var tiltMouseX=0;
var tiltMouseY=0;
var tiltMouseDown=false;
Giraffe.Tablet.convertRotationToMouse = function(canvas) {
Giraffe.Tablet.noramlizeOrientationEvents();
deviceOrientationHandler = function(tiltLR, tiltFB, dir, motUD) {
// var canvas = canvas;
console.log( "event: tiltLR="+tiltLR+", tiltFB="+tiltFB+", dir="+dir+", motUD="+motUD );
var mouseClick = false;
if(tiltLR!=0 && tiltLR!=null) {
if(tiltMouseDown==false) {
mouseClick=true;
}
tiltMouseX+=tiltLR;
}
if(tiltFB!=0 && tiltFB!=null) {
if(tiltMouseDown==false) {
mouseClick=true;
}
tiltMouseY+=tiltFB;
}
if(mouseClick==true) {
tiltMouseDown=true;
canvas.onMouseDown( {x:tiltMouseX,y:tiltMouseY } );
} else if(tiltLR==0 && tiltFB==0) {
tiltMouseDown=false;
canvas.onMouseUp( {x:tiltMouseX,y:tiltMouseY } );
}
canvas.onMouseMoved( {x:tiltMouseX,y:tiltMouseY } );
};
};
//http://www.html5rocks.com/en/tutorials/device/orientation/
Giraffe.Tablet.noramlizeOrientationEvents = function() {
if (window.DeviceOrientationEvent) {
// Listen for the deviceorientation event and handle the raw data
window.addEventListener('deviceorientation', function(eventData) {
// gamma is the left-to-right tilt in degrees, where right is positive
var tiltLR = eventData.gamma;
// beta is the front-to-back tilt in degrees, where front is positive
var tiltFB = eventData.beta;
// alpha is the compass direction the device is facing in degrees
var dir = eventData.alpha;
// deviceorientation does not provide this data
var motUD = null;
// call our orientation event handler
deviceOrientationHandler(tiltLR, tiltFB, dir, motUD);
}, false);
} else if (window.OrientationEvent) {
window.addEventListener('MozOrientation', function(eventData) {
// x is the left-to-right tilt from -1 to +1, so we need to convert to degrees
var tiltLR = eventData.x * 90;
// y is the front-to-back tilt from -1 to +1, so we need to convert to degrees
// We also need to invert the value so tilting the device towards us (forward)
// results in a positive value.
var tiltFB = eventData.y * -90;
// MozOrientation does not provide this data
var dir = null;
// z is the vertical acceleration of the device
var motUD = eventData.z;
// call our orientation event handler
deviceOrientationHandler(tiltLR, tiltFB, dir, motUD);
}, false);
} else {
console.log( "Not supported on your device or browser." );
}
}
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.quickStart = function( canvasId, fullscreen ) {
var canvas = document.getElementById(canvasId);
if( fullscreen ) {
try {
if (canvas.requestFullscreen) {
canvas.requestFullscreen();
} else if (canvas.webkitRequestFullscreen) {
canvas.webkitRequestFullscreen();
} else if (canvas.mozRequestFullScreen) {
canvas.mozRequestFullScreen();
} else if (canvas.msRequestFullscreen) {
canvas.msRequestFullscreen();
}
} catch(e) {
}
document.body.style.overflow="hidden";
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;
window.scrollTo(0,1);
}
var giraffeCanvas = new Canvas( canvasId ); // Register html canvas element
Giraffe.Interactive.setInteractive(giraffeCanvas); // If mouse interaction required
Giraffe.setAnimated(giraffeCanvas); // If animation required
giraffeCanvas.startAnimation(20,100,true); // frames per second, frame count in loop, looped
giraffeCanvas.width = canvas.width;
giraffeCanvas.height = canvas.height;
return giraffeCanvas;
};
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.addSelectBox = function(canvas) {
var selectBox = new RoundedRectangle(0,0,10,10,5).setColor("red").setFillColor("rgba(255,0,0,0.5)");
canvas.selectBox = selectBox;
canvas.onMousePressed = function(x,y) {
if(canvas.dragging.length==0) {
selectBox.x = x;
selectBox.y = y;
selectBox.width = 0;
selectBox.height = 0;
selectBox.visible = true;
}
};
canvas.selectBox.callBack = function() {};
canvas.onMouseOver = function(x,y) {
if(selectBox.visible) {
if(canvas.dragging.length==0) {
selectBox.width = x-selectBox.x;
selectBox.height = y-selectBox.y;
} else {
selectBox.visible = false;
}
}
};
canvas.onMouseReleased = function() {
if(selectBox.visible) {
selectBox.visible = false;
selectBox.callBack( selectBox );
}
};
selectBox.visible = false;
canvas.add( selectBox );
};
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.addSnapToGrid = function(canvas) {
var grid = new Composite(0,0);
for(var x=0;xgi;
if(g===this) continue;
g.x = Math.round(g.x/20)*20;
g.y = Math.round(g.y/20)*20;
}
};
};
//==============================================================================================================//
//==============================================================================================================//
//==============================================================================================================//
Giraffe.addRightClick = function(canvas) {
canvas.addEventListener('contextmenu', function(ev) {
ev.preventDefault();
//alert('success!');
return false;
}, false);
};
//==============================================================================================================//