diff --git a/example/example.js b/example/example.js index 3209b1d..94c51b5 100644 --- a/example/example.js +++ b/example/example.js @@ -1,8 +1,14 @@ +const canvasDimensions = { width: 700 * 2, height: 1200 * 2 }; + function getReferenceContext2d(element, transform) { const context = element.getContext("2d"); context.scale(transform.scale, transform.scale); context.translate(transform.x, transform.y); + context.translate(transform.rotation.x, transform.rotation.y); + context.rotate(transform.rotation.angle); + context.translate(-transform.rotation.x, -transform.rotation.y); + return context; } @@ -16,17 +22,21 @@ const referenceCanvas = document.getElementById("reference"); const farCanvas = document.getElementById("far"); const image = { data: document.createElement("img"), width: 320, height: 164 }; -const canvasDimensions = { width: 700, height: 1200 }; referenceCanvas.width = canvasDimensions.width; referenceCanvas.height = canvasDimensions.height; farCanvas.width = canvasDimensions.width; farCanvas.height = canvasDimensions.height; -const scale = canvasDimensions.width / image.width; -const focus = 10000; // 500000000 // breaks down in vanilla canvas +const scale = (canvasDimensions.width / image.width) * 0.5; +const focus = -100; // 500000000 // breaks down in vanilla canvas +const rotation = { + x: image.width / 2, + y: image.height / 2, + angle: Math.PI, +}; -const diff = -image.height * 0; +const diff = -image.height * 1; const mkImage = ({ x, y, image }) => ({ x, @@ -37,6 +47,7 @@ const mkImage = ({ x, y, image }) => ({ }); const images = [ + mkImage({ x: 0, y: focus - 2 * image.height, image }), mkImage({ x: 0, y: focus - 1 * image.height, image }), mkImage({ x: 0, y: focus + 0 * image.height, image }), mkImage({ x: 0, y: focus + 1 * image.height, image }), @@ -46,6 +57,7 @@ const images = [ ]; const rectangles = [ + { x: 10, y: focus - 200, width: 200, height: 30 }, { x: 10, y: focus + 20, width: 200, height: 30 }, { x: 100, y: focus + 250, width: 200, height: 30 }, { x: -10, y: focus - 10, width: 200, height: 30 }, @@ -60,12 +72,13 @@ const rectangles = [ const contextReference = getReferenceContext2d( document.getElementById("reference"), - { x: 0, y: -focus - diff, scale: scale } + { x: 0, y: -focus - diff, scale: scale, rotation: rotation } ); const contextFar = getFarContext2d(document.getElementById("far"), { x: 0, y: -focus - diff, scale: scale, + rotation: rotation, }); image.data.onload = function () { @@ -123,6 +136,41 @@ image.data.onload = function () { ctx.restore(); }); + // focus y + ctx.save(); + + ctx.beginPath(); + ctx.lineWidth = 8; + ctx.strokeStyle = "#0ac"; + ctx.moveTo(-2 * image.width, focus); + ctx.lineTo(2 * image.width, focus); + + ctx.stroke(); + ctx.restore(); + + // origo + ctx.save(); + + ctx.beginPath(); + ctx.lineWidth = 4; + ctx.strokeStyle = "#f00"; + const size = 16; + ctx.arc(0, 0, size, 0, 2 * Math.PI); + ctx.stroke(); + + ctx.beginPath(); + ctx.strokeStyle = "#0f0"; + ctx.moveTo(0, 0); + ctx.lineTo(2 * size, 0); + ctx.stroke(); + + ctx.beginPath(); + ctx.strokeStyle = "#00f"; + ctx.moveTo(0, 0); + ctx.lineTo(0, 2 * size); + ctx.stroke(); + + ctx.restore(); } render(contextReference); diff --git a/example/index.html b/example/index.html index 686e03c..eecc4a2 100644 --- a/example/index.html +++ b/example/index.html @@ -30,6 +30,6 @@