Skip to content

Restructure #7

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
88 changes: 85 additions & 3 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { far } from "../src/index.js";

function getReferenceContext2d(element, transform) {
const context = element.getContext("2d");
// Reset transform first
context.resetTransform();
// Then apply in same order: scale, rotate, translate
context.scale(transform.scale, transform.scale);
if (transform.rotation) {
context.rotate(transform.rotation);
}
context.translate(transform.x, transform.y);

return context;
}

function getFarContext2d(element, transform) {
const context = far.far(element, transform).getContext("2d");

return context;
return far(element, transform).getContext("2d");
}

const referenceCanvas = document.getElementById("reference");
Expand Down Expand Up @@ -70,6 +76,12 @@ const contextFar = getFarContext2d(document.getElementById("far"), {

image.data.onload = function () {
function render(ctx) {
// Clear the canvas first
ctx.save();
ctx.resetTransform();
ctx.clearRect(0, 0, canvasDimensions.width, canvasDimensions.height);
ctx.restore();

images.forEach((image, i) => {
ctx.save();

Expand Down Expand Up @@ -123,10 +135,80 @@ image.data.onload = function () {

ctx.restore();
});
// Draw transform tests last (at the top of canvas)
testTransforms(ctx);
}

// Run rendering
render(contextReference);
render(contextFar);
};
image.data.src =
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Saturn_from_Cassini_Orbiter_%282004-10-06%29.jpg/320px-Saturn_from_Cassini_Orbiter_%282004-10-06%29.jpg";

// Test transform operations
function testTransforms(ctx) {
// Save current state before switching to screen space
ctx.save();
ctx.resetTransform();

// Clear the test area
ctx.fillStyle = "#EEE";
ctx.fillRect(0, 0, 400, 200);

// Draw a grid for reference
ctx.strokeStyle = "#CCC";
for (let x = 0; x <= 400; x += 50) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, 200);
ctx.stroke();
}
for (let y = 0; y <= 200; y += 50) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(400, y);
ctx.stroke();
}

// Draw labels
ctx.fillStyle = "#000";
ctx.font = "12px sans-serif";
ctx.fillText("1. Blue: translate(50,50)", 10, 15);
ctx.fillText("2. Green: scale(2,2)", 10, 30);
ctx.fillText("3. Red: rotate(45°)", 10, 45);
ctx.fillText("4. Yellow: setTransform(skew)", 10, 60);

// Test 1: Basic translation
ctx.save();
ctx.translate(50, 50);
ctx.fillStyle = "#00F";
ctx.fillRect(0, 0, 30, 30);
ctx.restore();

// Test 2: Scale
ctx.save();
ctx.translate(100, 50);
ctx.scale(2, 2);
ctx.fillStyle = "#0F0";
ctx.fillRect(-15, -15, 15, 15);
ctx.restore();

// Test 3: Rotation
ctx.save();
ctx.translate(150, 50);
ctx.rotate(Math.PI / 4); // 45 degrees
ctx.fillStyle = "#F00";
ctx.fillRect(-10, -10, 20, 20);
ctx.restore();

// Test 4: setTransform (absolute)
ctx.save();
ctx.setTransform(1, 0.2, 0.2, 1, 250, 75);
ctx.fillStyle = "#FF0";
ctx.fillRect(0, 0, 30, 30);
ctx.restore();

// Restore original state (back to world space)
ctx.restore();
}
41 changes: 20 additions & 21 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<title>FarCanvas Example</title>
<style>
body {
background: #CEF;
.canvas-container {
display: flex;
gap: 20px;
margin: 20px;
}
div {
display: inline-block;
canvas {
border: 1px solid #333;
}
#reference_container {
border: solid green 1px;
}
#far_container {
border: solid red 1px;
.canvas-label {
text-align: center;
margin-bottom: 5px;
font-family: sans-serif;
}
</style>
</head>
<body>
<div>
<div id="reference_container" class="canvas_container">
<canvas id="reference" width="8" height="8"></canvas>
<div class="canvas-container">
<div>
<div class="canvas-label">Reference Canvas</div>
<canvas id="reference"></canvas>
</div>
<h3 style="text-align: center">reference</h3>
</div>
<div>
<div id="far_container" class="canvas_container">
<canvas id="far" width="8" height="8"></canvas>
<div>
<div class="canvas-label">Far Canvas</div>
<canvas id="far"></canvas>
</div>
<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
Loading