2020-12-19 01:27:50 +00:00
|
|
|
<template>
|
2020-12-27 02:42:26 +00:00
|
|
|
<div>
|
2021-03-27 04:16:15 +00:00
|
|
|
<div id="canvas-area">
|
2021-03-20 03:45:10 +00:00
|
|
|
<vue-draggable-resizable
|
|
|
|
style="z-index: 5; left: 220px"
|
2021-03-29 00:39:42 +00:00
|
|
|
:grid="[
|
|
|
|
$store.getters.currentAscii.blockHeight,
|
|
|
|
$store.getters.currentAscii.blockWidth,
|
|
|
|
]"
|
|
|
|
:w="canvas.width + $store.getters.currentAscii.blockWidth"
|
|
|
|
:h="canvas.height + $store.getters.currentAscii.blockHeight"
|
|
|
|
:draggable="$store.getters.getCurrentTool === 'default'"
|
2021-03-26 11:36:37 +00:00
|
|
|
@resizing="onCanvasResize"
|
|
|
|
@dragging="onCanvasDrag"
|
2021-03-20 03:45:10 +00:00
|
|
|
>
|
|
|
|
<canvas
|
|
|
|
ref="canvas"
|
|
|
|
id="canvas"
|
|
|
|
class="canvas"
|
2021-03-26 11:36:37 +00:00
|
|
|
:width="canvas.width"
|
|
|
|
:height="canvas.height"
|
2021-03-27 02:07:33 +00:00
|
|
|
@mousemove="canvasMouseMove"
|
|
|
|
@mousedown="canvasMouseDown"
|
|
|
|
@mouseup="canvasMouseUp"
|
2021-03-20 03:45:10 +00:00
|
|
|
></canvas>
|
|
|
|
</vue-draggable-resizable>
|
2020-12-27 02:42:26 +00:00
|
|
|
</div>
|
2020-12-19 01:27:50 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2021-01-02 01:52:44 +00:00
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
background: #eee;
|
|
|
|
}
|
|
|
|
|
2021-01-10 03:06:46 +00:00
|
|
|
.canvas {
|
|
|
|
position: absolute;
|
2021-03-29 00:39:42 +00:00
|
|
|
background: rgba(0, 0, 0, 0.8);
|
2021-01-10 03:06:46 +00:00
|
|
|
border: lightgrey 1px solid;
|
|
|
|
z-index: 0;
|
|
|
|
}
|
2021-01-02 01:52:44 +00:00
|
|
|
</style>
|
|
|
|
|
2020-12-19 01:27:50 +00:00
|
|
|
<script>
|
2021-01-10 03:06:46 +00:00
|
|
|
import Block from "../components/Block.vue";
|
2020-12-27 02:42:26 +00:00
|
|
|
|
2020-12-19 01:27:50 +00:00
|
|
|
export default {
|
2021-01-10 03:06:46 +00:00
|
|
|
name: "Editor",
|
2020-12-27 02:42:26 +00:00
|
|
|
components: { Block },
|
2021-01-02 01:52:44 +00:00
|
|
|
mounted() {
|
2021-03-29 00:39:42 +00:00
|
|
|
if (this.$store.getters.currentAscii.blocks) {
|
2021-03-13 05:27:07 +00:00
|
|
|
this.ctx = this.$refs.canvas.getContext("2d");
|
2021-03-27 02:07:33 +00:00
|
|
|
this.canvas.width =
|
2021-03-29 00:39:42 +00:00
|
|
|
this.$store.getters.currentAscii.width *
|
|
|
|
this.$store.getters.currentAscii.blockWidth;
|
2021-03-27 02:07:33 +00:00
|
|
|
this.canvas.height =
|
2021-03-29 00:39:42 +00:00
|
|
|
this.$store.getters.currentAscii.height *
|
|
|
|
this.$store.getters.currentAscii.blockHeight;
|
2021-03-27 02:07:33 +00:00
|
|
|
this.delayRedrawCanvas();
|
2021-03-27 04:16:15 +00:00
|
|
|
this.$store.commit("changeTool", "default");
|
2021-03-13 05:27:07 +00:00
|
|
|
}
|
2020-12-27 02:42:26 +00:00
|
|
|
},
|
2021-01-10 03:06:46 +00:00
|
|
|
created() {},
|
2020-12-27 02:42:26 +00:00
|
|
|
data: () => ({
|
2021-01-02 01:52:44 +00:00
|
|
|
ctx: null,
|
2021-01-09 01:57:48 +00:00
|
|
|
canvas: {
|
2021-03-26 11:36:37 +00:00
|
|
|
width: 512,
|
|
|
|
height: 512,
|
2021-01-09 01:57:48 +00:00
|
|
|
},
|
2021-03-29 00:39:42 +00:00
|
|
|
x: 0, // Ascii X and Y
|
|
|
|
y: 0, // Ascii X and Y
|
|
|
|
redraw: true, // Used to limit canvas redraw
|
2021-03-27 04:16:15 +00:00
|
|
|
canTool: false,
|
2020-12-27 02:42:26 +00:00
|
|
|
}),
|
|
|
|
computed: {
|
2021-03-13 05:27:07 +00:00
|
|
|
canvasStyle() {
|
2021-03-20 03:45:10 +00:00
|
|
|
return `width:${this.canvas.width};height:${this.canvas.height};`;
|
2021-01-23 02:50:32 +00:00
|
|
|
},
|
|
|
|
generateTitle() {
|
2021-03-29 00:39:42 +00:00
|
|
|
return this.$store.getters.currentAscii.title ?? "";
|
2021-03-13 05:27:07 +00:00
|
|
|
},
|
|
|
|
watchAsciiChange() {
|
|
|
|
return this.$store.getters.currentAscii;
|
2021-02-06 01:57:35 +00:00
|
|
|
},
|
2020-12-27 02:42:26 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2021-03-13 05:27:07 +00:00
|
|
|
watchAsciiChange(val, old) {
|
2021-03-27 02:07:33 +00:00
|
|
|
this.canvas.width =
|
2021-03-29 00:39:42 +00:00
|
|
|
this.$store.getters.currentAscii.width *
|
|
|
|
this.$store.getters.currentAscii.blockWidth;
|
2021-03-27 02:07:33 +00:00
|
|
|
this.canvas.height =
|
2021-03-29 00:39:42 +00:00
|
|
|
this.$store.getters.currentAscii.height *
|
|
|
|
this.$store.getters.currentAscii.blockHeight;
|
|
|
|
|
2021-03-27 02:07:33 +00:00
|
|
|
this.delayRedrawCanvas();
|
2021-03-27 04:16:15 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
document.title = `asciibird - ${this.$store.getters.currentAscii.title}`;
|
2021-03-27 02:07:33 +00:00
|
|
|
},
|
2020-12-27 02:42:26 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2021-01-09 01:57:48 +00:00
|
|
|
redrawCanvas() {
|
2021-03-29 00:39:42 +00:00
|
|
|
// Clears the whole canvas, we can maybe get a better way to check how far
|
|
|
|
// we need to clear the canvas
|
2021-03-26 11:36:37 +00:00
|
|
|
this.ctx.clearRect(0, 0, 10000, 10000);
|
2021-01-23 02:50:32 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
if (this.$store.getters.currentAscii.blocks.length) {
|
|
|
|
const BLOCK_WIDTH = this.$store.getters.currentAscii.blockWidth;
|
|
|
|
const BLOCK_HEIGHT = this.$store.getters.currentAscii.blockHeight;
|
2021-01-23 02:50:32 +00:00
|
|
|
|
2021-03-20 03:45:10 +00:00
|
|
|
// Position of the meta array
|
|
|
|
let x = 0;
|
|
|
|
let y = 0;
|
2021-01-23 02:50:32 +00:00
|
|
|
|
2021-03-20 03:45:10 +00:00
|
|
|
// Draws the actual rectangle
|
|
|
|
let canvasX = 0;
|
|
|
|
let canvasY = 0;
|
|
|
|
let curBlock = {};
|
2021-03-06 02:03:22 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
// hack font for ascii shout outs 2 beenz
|
2021-03-20 09:33:16 +00:00
|
|
|
this.ctx.font = "12.5px Hack";
|
2021-03-06 02:03:22 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
for (y = 0; y < this.$store.getters.currentAscii.height + 1; y++) {
|
2021-03-20 03:45:10 +00:00
|
|
|
canvasY = BLOCK_HEIGHT * y;
|
2021-01-09 01:57:48 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
for (x = 0; x < this.$store.getters.currentAscii.width + 1; x++) {
|
2021-03-20 03:45:10 +00:00
|
|
|
if (
|
2021-03-29 00:39:42 +00:00
|
|
|
this.$store.getters.currentAscii.blocks[y] &&
|
|
|
|
this.$store.getters.currentAscii.blocks[y][x]
|
2021-03-20 03:45:10 +00:00
|
|
|
) {
|
2021-03-29 00:39:42 +00:00
|
|
|
curBlock = Object.assign(
|
|
|
|
curBlock,
|
|
|
|
this.$store.getters.currentAscii.blocks[y][x]
|
|
|
|
);
|
2021-03-27 02:07:33 +00:00
|
|
|
|
2021-03-20 03:45:10 +00:00
|
|
|
canvasX = BLOCK_WIDTH * x;
|
2021-01-23 02:50:32 +00:00
|
|
|
|
2021-03-20 03:45:10 +00:00
|
|
|
// Background block
|
|
|
|
if (curBlock.bg !== null) {
|
2021-03-29 05:10:31 +00:00
|
|
|
this.ctx.fillStyle = this.$store.getters.mircColours[
|
2021-03-29 00:39:42 +00:00
|
|
|
curBlock.bg
|
|
|
|
];
|
2021-03-20 03:45:10 +00:00
|
|
|
this.ctx.fillRect(canvasX, canvasY, BLOCK_WIDTH, BLOCK_HEIGHT);
|
|
|
|
} else {
|
|
|
|
this.ctx.fillStyle = "rgba(0, 0, 200, 0)";
|
|
|
|
}
|
2021-03-13 05:27:07 +00:00
|
|
|
|
2021-03-20 03:45:10 +00:00
|
|
|
if (curBlock.char) {
|
|
|
|
if (curBlock.fg !== null) {
|
2021-03-29 05:10:31 +00:00
|
|
|
this.ctx.fillStyle = this.$store.getters.mircColours[
|
2021-03-29 00:39:42 +00:00
|
|
|
curBlock.fg
|
|
|
|
];
|
2021-03-20 03:45:10 +00:00
|
|
|
} else {
|
|
|
|
this.ctx.fillStyle = "#000000";
|
2021-03-13 05:27:07 +00:00
|
|
|
}
|
2021-03-20 03:45:10 +00:00
|
|
|
|
|
|
|
this.ctx.fillText(
|
|
|
|
curBlock.char,
|
2021-03-20 09:33:16 +00:00
|
|
|
canvasX - 1,
|
|
|
|
canvasY + BLOCK_HEIGHT - 3
|
2021-03-20 03:45:10 +00:00
|
|
|
);
|
2021-03-06 02:03:22 +00:00
|
|
|
}
|
2021-01-23 02:50:32 +00:00
|
|
|
}
|
2021-01-30 02:33:11 +00:00
|
|
|
}
|
2021-01-09 01:57:48 +00:00
|
|
|
}
|
2021-03-20 03:45:10 +00:00
|
|
|
}
|
2021-01-09 01:57:48 +00:00
|
|
|
|
2021-03-20 03:45:10 +00:00
|
|
|
this.ctx.stroke();
|
2021-03-27 04:16:15 +00:00
|
|
|
},
|
|
|
|
onCanvasResize(left, top, width, height) {
|
|
|
|
this.canvas.width = width;
|
|
|
|
this.canvas.height = height;
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
},
|
|
|
|
onCanvasDrag(left, top) {
|
|
|
|
// Update left and top in panel
|
|
|
|
},
|
|
|
|
|
|
|
|
// Mouse Up, Down and Move
|
|
|
|
canvasMouseUp() {
|
|
|
|
this.delayRedrawCanvas();
|
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
switch (this.$store.getters.getCurrentTool) {
|
2021-03-27 04:16:15 +00:00
|
|
|
case "brush":
|
|
|
|
this.canTool = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "eraser":
|
|
|
|
this.canTool = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "fill":
|
|
|
|
this.canTool = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
canvasMouseDown() {
|
2021-03-29 00:39:42 +00:00
|
|
|
if (
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y] &&
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y][this.x]
|
|
|
|
) {
|
|
|
|
switch (this.$store.getters.getCurrentTool) {
|
|
|
|
case "default":
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "fill":
|
|
|
|
this.fillTool();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "brush":
|
|
|
|
this.canTool = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "eraser":
|
|
|
|
this.canTool = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "dropper":
|
|
|
|
let curBlock = this.$store.getters.currentAscii.blocks[this.y][
|
|
|
|
this.x
|
|
|
|
];
|
|
|
|
|
|
|
|
if (this.$store.getters.getTargetingFg) {
|
2021-03-29 05:10:31 +00:00
|
|
|
this.$store.commit("changeColourFg", curBlock.fg);
|
2021-03-29 00:39:42 +00:00
|
|
|
}
|
2021-03-27 04:16:15 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
if (this.$store.getters.getTargetingBg) {
|
2021-03-29 05:10:31 +00:00
|
|
|
this.$store.commit("changeColourBg", curBlock.bg);
|
2021-03-29 00:39:42 +00:00
|
|
|
}
|
2021-03-27 04:16:15 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
this.$store.commit(
|
|
|
|
"changeTool",
|
|
|
|
this.$store.getters.getCurrentTool
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2021-03-27 04:16:15 +00:00
|
|
|
}
|
2021-01-09 01:57:48 +00:00
|
|
|
},
|
2021-03-27 02:07:33 +00:00
|
|
|
canvasMouseMove(e) {
|
2021-03-13 03:30:58 +00:00
|
|
|
if (e.offsetX >= 0) {
|
|
|
|
this.x = e.offsetX;
|
|
|
|
}
|
2021-03-13 05:27:07 +00:00
|
|
|
|
2021-03-13 03:30:58 +00:00
|
|
|
if (e.offsetY >= 0) {
|
|
|
|
this.y = e.offsetY;
|
|
|
|
}
|
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
this.x = Math.floor(this.x / this.$store.getters.currentAscii.blockWidth);
|
|
|
|
this.y = Math.floor(
|
|
|
|
this.y / this.$store.getters.currentAscii.blockHeight
|
|
|
|
);
|
|
|
|
|
2021-03-29 05:10:31 +00:00
|
|
|
this.$emit('coordsupdate', {x: this.x, y: this.y})
|
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
if (
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y] &&
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y][this.x]
|
|
|
|
) {
|
|
|
|
switch (this.$store.getters.getCurrentTool) {
|
|
|
|
case "brush":
|
|
|
|
if (this.canTool) {
|
|
|
|
if (this.$store.getters.getTargetingFg) {
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y][
|
2021-03-27 02:07:33 +00:00
|
|
|
this.x
|
2021-03-29 05:10:31 +00:00
|
|
|
].fg = this.$store.getters.getFgColour;
|
2021-03-27 02:07:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
if (this.$store.getters.getTargetingBg) {
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y][
|
2021-03-27 02:07:33 +00:00
|
|
|
this.x
|
2021-03-29 05:10:31 +00:00
|
|
|
].bg = this.$store.getters.getBgColour;
|
2021-03-27 02:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-29 00:39:42 +00:00
|
|
|
break;
|
2021-03-27 02:07:33 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
case "eraser":
|
|
|
|
if (this.canTool) {
|
|
|
|
if (this.$store.getters.getTargetingFg) {
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y][
|
|
|
|
this.x
|
|
|
|
].fg = null;
|
2021-03-27 02:07:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
if (this.$store.getters.getTargetingBg) {
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y][
|
|
|
|
this.x
|
|
|
|
].bg = null;
|
2021-03-27 02:07:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
if (this.$store.getters.getTargetingText) {
|
|
|
|
this.$store.getters.currentAscii.blocks[this.y][
|
|
|
|
this.x
|
|
|
|
].char = null;
|
2021-03-27 02:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-29 00:39:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-27 02:07:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
this.delayRedrawCanvas();
|
2021-03-13 03:30:58 +00:00
|
|
|
},
|
2021-03-27 02:07:33 +00:00
|
|
|
delayRedrawCanvas() {
|
|
|
|
if (this.redraw) {
|
|
|
|
this.redraw = false;
|
2021-03-29 00:39:42 +00:00
|
|
|
|
2021-03-27 02:07:33 +00:00
|
|
|
setTimeout(() => {
|
2021-03-29 00:39:42 +00:00
|
|
|
this.redraw = true;
|
2021-03-27 02:07:33 +00:00
|
|
|
this.redrawCanvas();
|
2021-03-27 04:16:15 +00:00
|
|
|
}, 8);
|
2021-03-27 02:07:33 +00:00
|
|
|
}
|
2021-03-26 11:36:37 +00:00
|
|
|
},
|
2021-03-27 04:24:05 +00:00
|
|
|
|
|
|
|
// TOOLS
|
2021-03-27 04:16:15 +00:00
|
|
|
fillTool() {
|
2021-03-29 00:39:42 +00:00
|
|
|
let fillStartBlock = this.$store.getters.currentAscii.blocks[this.y][
|
|
|
|
this.x
|
|
|
|
];
|
|
|
|
|
|
|
|
if (this.$store.getters.getTargetingBg) {
|
|
|
|
let fillSameBg = fillStartBlock.bg;
|
2021-03-20 03:45:10 +00:00
|
|
|
}
|
2021-03-27 02:07:33 +00:00
|
|
|
|
2021-03-29 00:39:42 +00:00
|
|
|
console.log(fillStartBlock);
|
2021-01-10 03:06:46 +00:00
|
|
|
},
|
2020-12-27 02:42:26 +00:00
|
|
|
},
|
2020-12-19 01:27:50 +00:00
|
|
|
};
|
|
|
|
</script>
|