I'm using canvas with predefined width and height, those are result of the available resource that I've to render.
The resources sizes are not always the same size as the screen (mobile device) so I "stretch" the document with "viewport" to get the optimal, kind of full screen look and fill appearance.
This one works good and the application is running correct (based on PlayN).
The problem appeared when I was requested to open "external" pages in the application using iFrame. It appears that iFrame inherits the "viewport" from parent document and the content of the website is get broken (zoomed in or zoomed out).
I checked a solution where we keep the "viewport=1" but play with CSS to stretch the canvas, the application looks good but all the mouse/touch events are got broken, they receives the real (zoomed) position but my application expects position that is based on original canvas size.
Example here.
Any idea how to stretch the canvas and still keep the coordinates correct for the original dimension?
<body>
<canvas id="canvas" width="100px" height="100px" style="border: 1px solid black"></canvas>
</body>
var can = document.getElementsByTagName("canvas")[0];
can.style.width = "300px";
can.style.height = "300px";
can.addEventListener('mousemove', function(evt) {
var mousePos = getMousePos(canvas, evt);
var message = 'x: ' + mousePos.x + ', y:' + mousePos.y;
writeMessage(canvas, message);
}, false);
function writeMessage(canvas, message) {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = '8pt Calibri';
context.fillStyle = 'black';
context.fillText(message, 10, 25);
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}