Skip to content

feature: support pi rotation #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 77 additions & 23 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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,
Expand All @@ -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 () {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ <h3 style="text-align: center">reference</h3>
<h3 style="text-align: center">far</h3>
</div>
<script type="application/javascript" src="../lib.web/index.js"></script>
<script type="application/javascript" src="./example.js"></script>
<script type="module" src="./example.js"></script>
</body>
</html>
Loading