asciibird/src/views/Editor.vue

290 lines
6.8 KiB
Vue
Raw Normal View History

2020-12-19 01:27:50 +00:00
<template>
<div>
2021-01-02 01:52:44 +00:00
<h1>
{{ currentAsciibirdMeta.title }} ({{ currentAsciibirdMeta.width }} /
{{ currentAsciibirdMeta.height }})
</h1>
<div id="canvas-area" style="position: relative">
2021-03-13 05:27:07 +00:00
<span>{{ x }}, {{ y }}</span>
<canvas
ref="grid"
id="grid"
width="5024"
height="5024"
class="gridCanvas"
></canvas>
2021-03-20 03:45:10 +00:00
<vue-draggable-resizable
style="z-index: 5; left: 220px"
:max-width="1000"
:max-height="1000"
:draggable="currentTool === 'default'"
>
<canvas
ref="canvas"
id="canvas"
width="1000"
height="1000"
:key="refreshCanvas"
class="canvas"
@mousemove="showCoordinates"
@mousedown="cavnasMouseDown"
@mouseup="cavnasMouseUp"
></canvas>
</vue-draggable-resizable>
</div>
2020-12-19 01:27:50 +00:00
</div>
</template>
2021-01-02 01:52:44 +00:00
<style>
body {
background: #eee;
}
.canvas {
position: absolute;
background: rgba(0, 0, 0, 0.2);
border: lightgrey 1px solid;
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2);
z-index: 0;
}
.gridCanvas {
position: absolute;
border: lightgrey 1px solid;
border-radius: 5px;
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2);
z-index: -1;
margin-left: -2502px;
margin-top: -2493px;
width: 5000px;
height: 5000px;
display: block;
2021-01-02 01:52:44 +00:00
}
</style>
2020-12-19 01:27:50 +00:00
<script>
import Block from "../components/Block.vue";
2020-12-19 01:27:50 +00:00
export default {
name: "Editor",
2021-03-13 05:27:07 +00:00
props: ["tab", "refresh"],
components: { Block },
2021-01-02 01:52:44 +00:00
mounted() {
this.currentAsciibirdMeta = this.$store.getters.currentAscii;
this.mircColors = this.$store.getters.mircColors;
2021-03-13 03:30:58 +00:00
2021-03-13 05:27:07 +00:00
if (this.currentAsciibirdMeta.blocks) {
this.ctx = this.$refs.canvas.getContext("2d");
this.gridCtx = this.$refs.grid.getContext("2d");
2021-03-20 03:45:10 +00:00
this.redrawCanvas();
2021-03-13 05:27:07 +00:00
}
},
created() {},
data: () => ({
text: "ASCII",
2021-01-23 02:50:32 +00:00
currentAsciibirdMeta: {
title: "Loading...",
width: 0,
height: 0,
},
2021-01-02 01:52:44 +00:00
data: {
message: "Hello Vue!",
2021-01-02 01:52:44 +00:00
vueCanvas: null,
},
mircColors: null,
2021-01-02 01:52:44 +00:00
ctx: null,
selectionMode: false,
startPosition: {
x: null,
y: null,
},
2021-01-09 01:57:48 +00:00
canvas: {
width: 2048,
height: 2048,
2021-01-09 01:57:48 +00:00
},
gridCtx: null,
2021-03-13 03:30:58 +00:00
x: 0,
2021-03-13 05:27:07 +00:00
y: 0,
refreshCanvas: 0,
2021-03-20 03:45:10 +00:00
currentTool: null,
}),
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-01-30 02:33:11 +00:00
return this.currentAsciibirdMeta.title ?? "";
},
2021-03-20 03:45:10 +00:00
// watchColorChange() {
// return this.$store.getters.getColor;
// },
2021-02-06 01:57:35 +00:00
watchToolChange() {
2021-03-20 03:45:10 +00:00
return this.$store.getters.getCurrentTool;
2021-03-13 05:27:07 +00:00
},
watchAsciiChange() {
return this.$store.getters.currentAscii;
2021-02-06 01:57:35 +00:00
},
},
watch: {
2021-03-13 05:27:07 +00:00
watchAsciiChange(val, old) {
2021-03-20 03:45:10 +00:00
this.currentAsciibirdMeta = val;
this.drawGrid();
this.redrawCanvas();
2021-02-06 01:57:35 +00:00
},
2021-03-20 03:45:10 +00:00
// watchColorChange(val) {
// console.log(JSON.stringify(val));
// },
2021-02-06 01:57:35 +00:00
watchToolChange(val) {
2021-03-20 03:45:10 +00:00
this.currentTool = val;
2021-03-13 05:27:07 +00:00
},
},
methods: {
getMircColor(index) {
2021-03-13 05:27:07 +00:00
return this.$store.getters.mircColors[index];
2021-01-02 01:52:44 +00:00
},
2021-01-09 01:57:48 +00:00
redrawCanvas() {
2021-01-23 02:50:32 +00:00
// Clears the whole canvas
2021-03-20 03:45:10 +00:00
this.ctx.clearRect(0, 0, 1000, 1000);
this.ctx.stroke();
2021-01-23 02:50:32 +00:00
2021-03-20 03:45:10 +00:00
this.drawGrid();
2021-01-23 02:50:32 +00:00
2021-03-20 03:45:10 +00:00
if (this.currentAsciibirdMeta.blocks.length) {
const BLOCK_WIDTH = this.currentAsciibirdMeta.blockWidth;
const BLOCK_HEIGHT = this.currentAsciibirdMeta.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 = {};
this.ctx.font = "14px Hack";
2021-03-20 03:45:10 +00:00
for (y = 0; y < this.currentAsciibirdMeta.height + 1; y++) {
canvasY = BLOCK_HEIGHT * y;
2021-01-09 01:57:48 +00:00
2021-03-20 03:45:10 +00:00
for (x = 0; x < this.currentAsciibirdMeta.width + 1; x++) {
if (
this.currentAsciibirdMeta.blocks[y] &&
this.currentAsciibirdMeta.blocks[y][x]
) {
curBlock = JSON.parse(
JSON.stringify(this.currentAsciibirdMeta.blocks[y][x])
);
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) {
this.ctx.fillStyle = this.mircColors[curBlock.bg];
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) {
this.ctx.fillStyle = this.mircColors[curBlock.fg];
} 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,
canvasX,
canvasY + BLOCK_HEIGHT
2021-03-20 03:45:10 +00:00
);
this.ctx.stroke();
}
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-01-09 01:57:48 +00:00
},
2021-01-07 23:53:28 +00:00
processMouseDown(e) {
2021-01-09 01:57:48 +00:00
// this.canvasClass(e)
2021-01-02 01:52:44 +00:00
this.selectionMode = true;
this.startPosition.x = e.clientX;
this.startPosition.y = e.clientY;
},
2021-01-07 23:53:28 +00:00
processMouseMove(e) {
2021-03-13 03:30:58 +00:00
// if (this.selectionMode) {
// }
2021-01-02 01:52:44 +00:00
},
2021-01-07 23:53:28 +00:00
processMouseUp(e) {
2021-01-02 01:52:44 +00:00
this.selectionMode = false;
this.startPosition.x = null;
this.startPosition.y = null;
},
2021-03-13 03:30:58 +00:00
showCoordinates(e) {
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-13 05:27:07 +00:00
this.x = Math.floor(this.x / this.currentAsciibirdMeta.blockWidth);
this.y = Math.floor(this.y / this.currentAsciibirdMeta.blockHeight);
2021-03-13 03:30:58 +00:00
},
cavnasMouseDown() {
2021-03-20 03:45:10 +00:00
switch (this.currentTool) {
case "mouse":
break;
case "brush":
if (
this.currentAsciibirdMeta.blocks[this.y] &&
this.currentAsciibirdMeta.blocks[this.y][this.x]
)
this.currentAsciibirdMeta.blocks[this.y][
this.x
].bg = this.$store.getters.getBgColor;
break;
}
2021-03-13 03:30:58 +00:00
},
cavnasMouseUp() {
this.redrawCanvas();
},
drawGrid() {
2021-01-23 02:50:32 +00:00
const width = 5000;
const height = 5000;
2021-02-06 01:22:16 +00:00
const s = 26;
const sW = 16;
2021-01-23 02:50:32 +00:00
const pL = s;
const pT = s;
const pR = s;
const pB = s;
2021-03-13 05:27:07 +00:00
if (this.gridCtx) {
this.gridCtx.clearRect(0, 0, width, height);
2021-03-13 05:27:07 +00:00
this.gridCtx.strokeStyle = "lightgrey";
this.gridCtx.lineWidth = 0.333;
this.gridCtx.beginPath();
for (let x = pL; x <= width - pR; x += sW) {
this.gridCtx.moveTo(x, pT);
this.gridCtx.lineTo(x, height - pB);
}
for (let y = pT; y <= height - pB; y += s) {
this.gridCtx.moveTo(pL, y);
this.gridCtx.lineTo(width - pR, y);
}
this.gridCtx.stroke();
}
},
},
2020-12-19 01:27:50 +00:00
};
</script>