Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmebasti committed Apr 29, 2022
1 parent 3a82438 commit dcaefcd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Set the default behavior for line endings.
* text=auto eol=lf
9 changes: 8 additions & 1 deletion src/modules/view/divCanvas/divCanvas.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
.canvas {
display: flex;
flex-direction: column;
}

.row {
flex: 1;

display: flex;
}

.border {
.cell {
flex: 1;
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/view/divCanvas/divCanvas.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="canvas" style={canvasStyle}>
<div for:each={canvas} for:item="row" key={row.key} class="row" style={rowHeight}>
<div for:each={row} for:item="pixel" key={pixel.key} class="border" style={border}>
<div for:each={canvas} for:item="row" key={row.key} class="row">
<div for:each={row} for:item="pixel" key={pixel.key} class="cell" style={cellBorder}>
<div class="pixel"
style={pixel.style}
data-x={pixel.x} data-y={pixel.y}
Expand Down
23 changes: 9 additions & 14 deletions src/modules/view/divCanvas/divCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@ import { LightningElement, api } from "lwc";

export default class DivCanvas extends LightningElement {
@api canvas = [];
@api scale = 20;
@api scale;
@api background;
_border;
@api border;

@api set border(value) {
this._border = 'border: ' + value;
get cellBorder() {
return 'border: ' + this.border;
}

get border() {
return this._border;
get canvasStyle() {
return `
width: ${this.canvas.width * this.scale}px;
height: ${this.canvas.height * this.scale}px;
background: ${this.background}`;
}

dispatchClick({target: {dataset: {x, y}}}) {
x = Number(x);
y = Number(y);
this.dispatchEvent(new CustomEvent('pixelclick', {detail: {x, y}}))
}

get canvasStyle() {
return `width: ${this.scale * this.canvas.width}px; background: ${this.background}`;
}

get rowHeight() {
return `height: ${this.scale}px`;
}
}
20 changes: 10 additions & 10 deletions src/modules/view/divCanvas/model/canvas.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export default class Canvas extends Array {
static get [Symbol.species]() { return Array; }

constructor(width, height, color) {
super();
array(height).forEach((y) => this.push(new Row(y, color, width)));
super(...array(height).map((y) => new Row(y, color, width)));
}

draw(x, y, shape, color) {
Expand Down Expand Up @@ -86,18 +87,17 @@ class Row extends Array {
}
}

function newPixel(x, y, color) {
function newPixel(x, y, initial) {
return {
x, y, color,
x, y, initial,
key: y + '_' + x,
default: color,
style: 'background-color: ' + color,
paint: function(color = this.default) {
paint: function(color = initial) {
this.color = color;
this.style = 'background-color: ' + color;
this.style = color ? 'background-color: ' + color : undefined;
return this;
},
get empty() { return this.color === this.default; }
};
get empty() { return this.color === this.initial; }
}.paint();
}

function array(n) {
Expand Down
1 change: 1 addition & 0 deletions src/modules/view/divCanvas/model/shape.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default class Shape extends Array {
static get [Symbol.species]() { return Array; }

clone() {
return new Shape(...this);
Expand Down

0 comments on commit dcaefcd

Please sign in to comment.