Skip to content
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
28 changes: 28 additions & 0 deletions examples/feat-render-canvas.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<body >
<div id="myCanvas">

</div>
</canvas>
</body>
<script type="module">
import brickengine from '../src/index.js';
const {
renders
} = brickengine;
const canvasRender = renders.canvas({
weight: 40,
lines: 10,
columns: 10,
})// factory
console.log(canvasRender)
canvasRender.mount("myCanvas")// id
canvasRender.writePixel(0, 2)
canvasRender.writePixel(0, 1)
canvasRender.writePixel(0, 0)
setTimeout(() => canvasRender.erasePixel(0, 0)
, 3000)

</script>
</html>
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as renders from './renders/index.js'

export default {
renders
}
59 changes: 59 additions & 0 deletions src/renders/canvas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export default ({
weight,
lines,
columns
}) => {
const canvas = document.createElement('canvas');
canvas.setAttribute('width', weight*columns)
canvas.setAttribute('height', weight*lines)
const ctx = canvas.getContext('2d')

function writePixel(x, y) {
const offset = (multiplier) =>
(weight - weight*multiplier)/2;
ctx.fillStyle = "#000";
ctx.fillRect(
x*weight,
y*weight,
weight,
weight,
);

ctx.fillStyle = "#CCC";
ctx.fillRect(
x*weight + offset(0.9),
y*weight + offset(0.9),
weight*0.9,
weight*0.9,
);

ctx.fillStyle = "#000";
ctx.fillRect(
x*weight + offset(0.7),
y*weight + offset(0.7),
weight*0.7,
weight*0.7
);
}

function mount(elementId) {
const target = document.getElementById(elementId);
if (target) {
target.appendChild(canvas)
}
}

function erasePixel(x, y) {
ctx.clearRect(
x*weight,
y*weight,
weight,
weight,
);
}
return {
writePixel,
erasePixel,
mount,
}
}
3 changes: 3 additions & 0 deletions src/renders/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { default as canvas } from './canvas.js'

export { canvas }