Last active
August 29, 2015 14:01
-
-
Save maxwihlborg/a6801f6669224f98f1e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var canvas = document.createElement("canvas"); | |
canvas.width = 200; canvas.height = 200; | |
var dispCanvas = document.createElement("canvas"); | |
dispCanvas.width = 400; dispCanvas.height = 400; | |
var dispCtx = dispCanvas.getContext("2d"); | |
// setup pixel scaling | |
dispCtx.imageSmoothingEnabled = false; | |
dispCtx.oImageSmoothingEnabled = false; | |
dispCtx.mozImageSmoothingEnabled = false; | |
dispCtx.webkitImageSmoothingEnabled = false; | |
var scale = dispCanvas.width/canvas.width; | |
var ox = oy = 0, mouse = {x:0, y:0}; | |
(function() { | |
var el = dispCanvas; | |
do{ | |
ox += el.offsetLeft; | |
oy += el.offsetTop; | |
} while(el = el.offsetParent) | |
})(); | |
dispCanvas.addEventListener("mousedown", onDown, false); | |
dispCanvas.addEventListener("touchstart", onTouch, false;) | |
function onDown(mouseEvent) { | |
var x = mouseEvent.pageX; | |
var y = mouseEvent.pageY; | |
touchOrClick(x,y); | |
} | |
function onTouch(touchEvent) { | |
var e = touchEvent.targetTouches[0]; | |
var x = e.pageX; | |
var y = e.pageY; | |
touchOrClick(x, y); | |
} | |
function touchOrClick(x,y) { | |
mouse.x = (x - ox)/scale; | |
mouse.y = (y - oy)/scale; | |
} | |
function swap() { | |
dispCtx.drawImage(canvas, 0, 0, dispCanvas.width, dispCanvas.height); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment