Skip to content
Snippets Groups Projects
Commit 7d1f8b8a authored by Chris Hallberg's avatar Chris Hallberg
Browse files

Minimap dragging.

parent c08b6b20
Branches
Tags
No related merge requests found
...@@ -63,17 +63,47 @@ var Zoomy = { ...@@ -63,17 +63,47 @@ var Zoomy = {
mouseHandle: function(e) { mouseHandle: function(e) {
if(!Zoomy.mouseDown) return; if(!Zoomy.mouseDown) return;
e.preventDefault(); e.preventDefault();
var bounds = Zoomy.canvas.getBoundingClientRect();
var mx = e.type.match("touch") var mx = e.type.match("touch")
? e.targetTouches[0].pageX ? e.targetTouches[0].pageX
: e.pageX; : e.pageX;
mx -= Zoomy.canvas.offsetLeft; mx -= bounds.x;
var my = e.type.match("touch") var my = e.type.match("touch")
? e.targetTouches[0].pageY ? e.targetTouches[0].pageY
: e.pageY; : e.pageY;
my -= Zoomy.canvas.offsetTop; my -= bounds.y + window.scrollY;
if(typeof Zoomy.mouse !== "undefined") { if(typeof Zoomy.mouse !== "undefined") {
Zoomy.image.x = Math.floor(Zoomy.image.x + mx - Zoomy.mouse.x); var xdiff = mx - Zoomy.mouse.x;
Zoomy.image.y = Math.floor(Zoomy.image.y + my - Zoomy.mouse.y); var ydiff = my - Zoomy.mouse.y;
if(Zoomy.minimap != null
&& mx > Zoomy.minimap.rect.x
&& mx < Zoomy.minimap.rect.x+Zoomy.minimap.rect.w
&& my > Zoomy.minimap.rect.y
&& my < Zoomy.minimap.rect.y+Zoomy.minimap.rect.h) {
var ratio = Zoomy.image.rwidth / Zoomy.minimap.width;
switch(Zoomy.image.angle % Math.TWO_PI) {
case 0:
xdiff *= -ratio;
ydiff *= -ratio;
break;
case Math.HALF_PI: // On right side
var xtemp = xdiff;
xdiff = ydiff * ratio;
ydiff = xtemp * -ratio;
break;
case Math.PI: // Upside-down
xdiff *= ratio;
ydiff *= ratio;
break;
default: // On left side
var xtemp = xdiff;
xdiff = ydiff * -ratio;
ydiff = xtemp * ratio;
break;
}
}
Zoomy.image.x = Math.floor(Zoomy.image.x + xdiff);
Zoomy.image.y = Math.floor(Zoomy.image.y + ydiff);
Zoomy.enforceBounds(); Zoomy.enforceBounds();
Zoomy.draw(); Zoomy.draw();
} }
...@@ -117,19 +147,18 @@ var Zoomy = { ...@@ -117,19 +147,18 @@ var Zoomy = {
this.image.rheight this.image.rheight
); );
this.context.restore(); this.context.restore();
// Minimap // Minimap
if(this.minimap == null) { if(this.minimap == null) {
this.minimap = this.initMinimap(); this.minimap = this.initMinimap();
} }
this.context.save();
this.context.translate(this.minimap.x, this.minimap.y);
this.context.drawImage( this.context.drawImage(
this.image.content, this.image.content,
0, 0, this.minimap.x,
this.minimap.y,
this.minimap.width, this.minimap.width,
this.minimap.height this.minimap.height
); );
this.context.strokeStyle = "#00F";
var hLength = (this.width / this.image.rwidth) * this.minimap.width; var hLength = (this.width / this.image.rwidth) * this.minimap.width;
var vLength = (this.height / this.image.rheight) * this.minimap.height; var vLength = (this.height / this.image.rheight) * this.minimap.height;
...@@ -165,11 +194,19 @@ var Zoomy = { ...@@ -165,11 +194,19 @@ var Zoomy = {
ydiff = 0; ydiff = 0;
drawHeight = this.minimap.height; drawHeight = this.minimap.height;
} }
this.minimap.rect = {
x: this.minimap.x+Math.floor(Math.max(0, xdiff)),
y: this.minimap.y+Math.floor(Math.max(0, ydiff)),
w: Math.ceil(drawWidth),
h: Math.ceil(drawHeight)
};
this.context.save();
this.context.strokeStyle = "#00F";
this.context.strokeRect( this.context.strokeRect(
Math.max(0, xdiff), this.minimap.rect.x,
Math.max(0, ydiff), this.minimap.rect.y,
drawWidth, this.minimap.rect.w,
drawHeight this.minimap.rect.h
); );
this.context.restore(); this.context.restore();
}, },
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment