2021-08-07 05:36:32 +00:00
|
|
|
<template>
|
|
|
|
<div>
|
2021-08-15 01:20:58 +00:00
|
|
|
<t-card class="overflow-x-scroll h-full">
|
|
|
|
<div :style="`height: ${blocksWidthHeight.h}px;width: ${blocksWidthHeight.w}px;`">
|
|
|
|
<canvas
|
|
|
|
ref="brushcanvas"
|
|
|
|
id="brushcanvas"
|
|
|
|
class="brushcanvas"
|
|
|
|
@mousemove="canvasMouseMove"
|
|
|
|
@mouseup="disable"
|
|
|
|
@mousedown.left="addBlock"
|
|
|
|
@mousedown.right="eraseBlock"
|
|
|
|
@contextmenu.prevent
|
|
|
|
:width="blocksWidthHeight.w"
|
|
|
|
:height="blocksWidthHeight.h"
|
|
|
|
@mouseenter="disableToolbarMoving"
|
|
|
|
@mouseleave="enableToolbarMoving"
|
|
|
|
/>
|
|
|
|
</div>
|
2021-08-07 05:36:32 +00:00
|
|
|
</t-card>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import {
|
|
|
|
mircColours99,
|
|
|
|
blockWidth,
|
|
|
|
blockHeight,
|
|
|
|
getBlocksWidth,
|
|
|
|
filterNullBlocks,
|
|
|
|
} from "../../ascii";
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "MainBrushCanvas",
|
2021-08-14 06:41:42 +00:00
|
|
|
created() {
|
|
|
|
window.addEventListener('load', () => {
|
|
|
|
// Fixes the font on load issue
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
})
|
|
|
|
},
|
2021-08-07 05:36:32 +00:00
|
|
|
mounted() {
|
2021-08-07 23:45:37 +00:00
|
|
|
this.ctx = this.canvasRef.getContext("2d");
|
2021-08-16 04:10:06 +00:00
|
|
|
this.delayRedrawCanvas();
|
2021-08-07 05:36:32 +00:00
|
|
|
},
|
|
|
|
data: () => ({
|
|
|
|
ctx: null,
|
|
|
|
redraw: true,
|
|
|
|
erasing: false,
|
|
|
|
drawing: false,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
}),
|
|
|
|
computed: {
|
2021-08-14 06:41:42 +00:00
|
|
|
blockWidth() {
|
|
|
|
return blockWidth * this.blockSizeMultiplier;
|
|
|
|
},
|
|
|
|
blockHeight() {
|
|
|
|
return blockHeight * this.blockSizeMultiplier;
|
|
|
|
},
|
|
|
|
blockSizeMultiplier() {
|
|
|
|
return this.$store.getters.blockSizeMultiplier
|
|
|
|
},
|
2021-08-07 05:36:32 +00:00
|
|
|
currentAscii() {
|
|
|
|
return this.$store.getters.currentAscii;
|
|
|
|
},
|
|
|
|
toolbarState() {
|
|
|
|
return this.$store.getters.toolbarState;
|
|
|
|
},
|
|
|
|
isTargettingBg() {
|
|
|
|
return this.$store.getters.isTargettingBg;
|
|
|
|
},
|
|
|
|
isTargettingFg() {
|
|
|
|
return this.$store.getters.isTargettingFg;
|
|
|
|
},
|
|
|
|
isTargettingChar() {
|
|
|
|
return this.$store.getters.isTargettingChar;
|
|
|
|
},
|
|
|
|
canFg() {
|
|
|
|
return this.$store.getters.isTargettingFg;
|
|
|
|
},
|
|
|
|
canBg() {
|
|
|
|
return this.$store.getters.isTargettingBg;
|
|
|
|
},
|
|
|
|
canText() {
|
|
|
|
return this.$store.getters.isTargettingChar;
|
|
|
|
},
|
|
|
|
currentFg() {
|
|
|
|
return this.$store.getters.currentFg;
|
|
|
|
},
|
|
|
|
currentBg() {
|
|
|
|
return this.$store.getters.currentBg;
|
|
|
|
},
|
|
|
|
currentChar() {
|
|
|
|
return this.$store.getters.currentChar;
|
|
|
|
},
|
|
|
|
brushSizeHeight() {
|
|
|
|
return this.$store.getters.brushSizeHeight;
|
|
|
|
},
|
|
|
|
brushSizeWidth() {
|
|
|
|
return this.$store.getters.brushSizeWidth;
|
|
|
|
},
|
|
|
|
brushSizeType() {
|
|
|
|
return this.$store.getters.brushSizeType;
|
|
|
|
},
|
|
|
|
options() {
|
|
|
|
return this.$store.getters.options;
|
|
|
|
},
|
|
|
|
brushBlocks() {
|
|
|
|
return this.$store.getters.brushBlocks;
|
|
|
|
},
|
|
|
|
blocksWidthHeight() {
|
|
|
|
return {
|
|
|
|
w: this.getBlocksWidth(this.brushBlocks) * blockWidth,
|
|
|
|
h: this.brushBlocks.length * blockHeight,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mircColours() {
|
|
|
|
return mircColours99;
|
|
|
|
},
|
2021-08-07 23:45:37 +00:00
|
|
|
canvasRef() {
|
|
|
|
return this.$refs.brushcanvas;
|
2021-11-20 00:38:29 +00:00
|
|
|
},
|
|
|
|
gridView() {
|
|
|
|
return this.toolbarState.gridView;
|
|
|
|
},
|
2021-08-07 05:36:32 +00:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
brushBlocks() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
currentAscii() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
brushSizeHeight() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
brushSizeWidth() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
isTargettingBg() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
isTargettingFg() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
isTargettingChar() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
currentFg() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
currentBg() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
currentChar() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
2021-08-14 06:41:42 +00:00
|
|
|
blockSizeMultiplier() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
2021-11-20 00:38:29 +00:00
|
|
|
gridView(val, old) {
|
|
|
|
if (val !== old) {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
}
|
|
|
|
},
|
2021-08-07 05:36:32 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getBlocksWidth(blocks) {
|
|
|
|
return getBlocksWidth(blocks);
|
|
|
|
},
|
|
|
|
filterNullBlocks(blocks) {
|
|
|
|
return filterNullBlocks(blocks);
|
|
|
|
},
|
2021-11-20 00:38:29 +00:00
|
|
|
drawGrid() {
|
|
|
|
let ctx = this.ctx;
|
|
|
|
let w = this.canvasRef.width;
|
|
|
|
let h = this.canvasRef.height;
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
|
|
|
for (var x = 0; x <= w; x += blockWidth) {
|
|
|
|
ctx.moveTo(x, 0);
|
|
|
|
ctx.lineTo(x, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.strokeStyle = "rgba(0, 0, 0, 1)";
|
|
|
|
ctx.lineWidth = 1;
|
|
|
|
ctx.setLineDash([1]);
|
|
|
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
for (var y = 0; y <= h; y += blockHeight) {
|
|
|
|
ctx.moveTo(0, y);
|
|
|
|
ctx.lineTo(w, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.stroke();
|
|
|
|
},
|
|
|
|
|
2021-08-07 05:36:32 +00:00
|
|
|
drawPreview() {
|
2021-08-07 23:45:37 +00:00
|
|
|
this.ctx.clearRect(0, 0, this.canvasRef.width, this.canvasRef.height);
|
2021-08-07 05:36:32 +00:00
|
|
|
this.ctx.fillStyle = this.mircColours[1];
|
|
|
|
|
|
|
|
// hack font for ascii shout outs 2 beenz
|
|
|
|
this.ctx.font = "13px Hack";
|
|
|
|
|
|
|
|
let y = 0;
|
|
|
|
let x = 0;
|
|
|
|
|
|
|
|
// Get middle block
|
|
|
|
if (this.brushBlocks) {
|
|
|
|
let blocksWidth = this.getBlocksWidth(this.brushBlocks);
|
|
|
|
for (y = 0; y < this.brushBlocks.length; y++) {
|
|
|
|
for (x = 0; x < blocksWidth; x++) {
|
|
|
|
if (this.brushBlocks[y] && this.brushBlocks[y][x]) {
|
|
|
|
const curBlock = this.brushBlocks[y][x];
|
|
|
|
|
2021-12-17 08:47:38 +00:00
|
|
|
if (curBlock.bg !== undefined && this.isTargettingBg) {
|
2021-08-07 05:36:32 +00:00
|
|
|
this.ctx.fillStyle = this.mircColours[curBlock.bg];
|
|
|
|
|
|
|
|
this.ctx.fillRect(
|
2021-08-12 01:52:40 +00:00
|
|
|
x * blockWidth,
|
|
|
|
y * blockHeight,
|
|
|
|
blockWidth,
|
|
|
|
blockHeight
|
2021-08-07 05:36:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:47:38 +00:00
|
|
|
if (curBlock.fg !== undefined && this.isTargettingFg) {
|
2021-08-07 05:36:32 +00:00
|
|
|
this.ctx.fillStyle = this.mircColours[curBlock.fg];
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:47:38 +00:00
|
|
|
if (curBlock.char !== undefined && this.isTargettingChar) {
|
2021-08-07 05:36:32 +00:00
|
|
|
this.ctx.fillStyle = this.mircColours[curBlock.fg];
|
|
|
|
this.ctx.fillText(
|
|
|
|
curBlock.char,
|
2021-08-12 09:36:35 +00:00
|
|
|
x * blockWidth,
|
2021-08-12 01:52:40 +00:00
|
|
|
y * blockHeight + blockHeight - 3
|
2021-08-07 05:36:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-20 00:38:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (this.gridView) {
|
|
|
|
this.drawGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-07 05:36:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
delayRedrawCanvas() {
|
|
|
|
if (this.redraw) {
|
|
|
|
this.redraw = false;
|
2021-10-02 03:48:39 +00:00
|
|
|
var _this = this;
|
|
|
|
setTimeout(function(){
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
_this.drawPreview();
|
|
|
|
_this.redraw = true;
|
|
|
|
});
|
|
|
|
}, 1000/this.options.fps)
|
2021-08-07 05:36:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// Basic block editing
|
|
|
|
canvasMouseMove(e) {
|
|
|
|
if (e.offsetX >= 0) {
|
|
|
|
this.x = e.offsetX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.offsetY >= 0) {
|
|
|
|
this.y = e.offsetY;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.x = Math.floor(this.x / blockWidth);
|
|
|
|
this.y = Math.floor(this.y / blockHeight);
|
|
|
|
|
|
|
|
if (this.erasing) {
|
|
|
|
this.brushBlocks[this.y][this.x] = {
|
|
|
|
bg: this.canBg ? null : this.currentFg,
|
|
|
|
fg: this.canFg ? null : this.currentBg,
|
|
|
|
char: this.canText ? null : this.currentChar,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.drawing) {
|
|
|
|
this.brushBlocks[this.y][this.x] = {
|
|
|
|
bg: this.canBg ? this.currentBg : null,
|
|
|
|
fg: this.canFg ? this.currentFg : null,
|
|
|
|
char: this.canText ? this.currentChar : null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
disable() {
|
|
|
|
this.erasing = false;
|
|
|
|
this.drawing = false;
|
|
|
|
},
|
|
|
|
addBlock() {
|
|
|
|
this.drawing = true;
|
|
|
|
this.brushBlocks[this.y][this.x] = {
|
|
|
|
bg: this.canBg ? this.currentBg : null,
|
|
|
|
fg: this.canFg ? this.currentFg : null,
|
|
|
|
char: this.canText ? this.currentChar : null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
eraseBlock(e) {
|
|
|
|
this.erasing = true;
|
|
|
|
this.brushBlocks[this.y][this.x] = {
|
|
|
|
bg: this.canBg ? null : this.currentFg,
|
|
|
|
fg: this.canFg ? null : this.currentBg,
|
|
|
|
char: this.canText ? null : this.currentChar,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
disableToolbarMoving() {
|
|
|
|
this.$store.commit("changeToolBarDraggable", false);
|
|
|
|
},
|
|
|
|
enableToolbarMoving() {
|
|
|
|
// Save the blocks when the mouse leaves the canvas area
|
|
|
|
// To avoid one block history changes
|
|
|
|
this.disable()
|
|
|
|
this.$store.commit("brushBlocks", this.brushBlocks);
|
|
|
|
this.$store.commit("changeToolBarDraggable", true);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|