diff --git a/example/example.js b/example/example.js index 3209b1d..87e07bc 100644 --- a/example/example.js +++ b/example/example.js @@ -1,13 +1,22 @@ +import { far } from "../src/index.js"; + +const canvasDimensions = { width: 700, height: 1200 }; + 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); + // context.scale(-1, -1); + return context; } function getFarContext2d(element, transform) { - const context = far.far(element, transform).getContext("2d"); + const context = far(element, transform).getContext("2d"); return context; } @@ -16,17 +25,23 @@ 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; +referenceCanvas.width = canvasDimensions.width * 2; +referenceCanvas.height = canvasDimensions.height * 2; +farCanvas.width = canvasDimensions.width * 2; +farCanvas.height = canvasDimensions.height * 2; const scale = canvasDimensions.width / image.width; -const focus = 10000; // 500000000 // breaks down in vanilla canvas +const focus = -0; // 500000000 // breaks down in vanilla canvas +const rotation = { + x: image.width / 2, + // y: focus + image.height / 2, + y: focus, + angle: Math.PI, + // angle: 0, +}; -const diff = -image.height * 0; +const diff = -image.height * 3; const mkImage = ({ x, y, image }) => ({ x, @@ -37,35 +52,38 @@ const mkImage = ({ x, y, image }) => ({ }); const images = [ - mkImage({ x: 0, y: focus - 1 * image.height, image }), + // 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 }), + // mkImage({ x: 0, y: focus + 1 * image.height, image }), mkImage({ x: 0, y: focus + 2 * image.height, image }), - mkImage({ x: 0, y: focus + 3 * image.height, image }), - mkImage({ x: 0, y: focus + 4 * image.height, image }), + // mkImage({ x: 0, y: focus + 3 * image.height, image }), + // mkImage({ x: 0, y: focus + 4 * image.height, image }), ]; const rectangles = [ - { 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 }, - { x: 100, y: focus + 400, width: 200, height: 30 }, - { - x: 0, - y: focus + 2 * image.height, - width: image.width, - height: image.height, - }, + { 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 }, + // { x: 100, y: focus + 400, width: 200, height: 30 }, + // { + // x: 0, + // y: focus + 2 * image.height, + // width: image.width, + // height: image.height, + // }, ]; 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 +141,42 @@ 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 @@