asciibird/src/views/Editor.vue

169 lines
4.4 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>
<pre><small>{{ JSON.stringify(currentAsciibirdMeta) }}</small></pre>
2021-01-02 01:52:44 +00:00
<div>
<canvas
:ref="generateCanvasId"
:id="generateCanvasId"
2021-01-07 23:53:28 +00:00
@mousedown="processMouseDown"
@mousemove="processMouseMove"
@mouseup="processMouseUp"
2021-01-02 01:52:44 +00:00
v-bind:class="dataFieldClass"
2021-01-07 23:32:19 +00:00
class="border-gray-500"
2021-01-02 01:52:44 +00:00
></canvas>
<!-- <span v-for="(yValue, y) in currentAsciibirdMeta.blocks" :key="y">
<span v-for="(xValue, x) in currentAsciibirdMeta.blocks" :key="x">
2021-01-02 01:52:44 +00:00
<Block :data="currentAsciibirdMeta.blocks[y][x]" v-if="currentAsciibirdMeta.blocks.length" :blockWidth="currentAsciibirdMeta.blockWidth" :blockHeight="currentAsciibirdMeta.blockHeight" />
</span>
2021-01-02 01:52:44 +00:00
</span> -->
</div>
2020-12-19 01:27:50 +00:00
</div>
</template>
2021-01-02 01:52:44 +00:00
<style>
body {
margin: 2rem;
background: #eee;
}
#canvas {
background: white;
box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.2);
}
</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',
components: { Block },
2021-01-02 01:52:44 +00:00
mounted() {
this.currentAsciibirdMeta = this.$store.state.asciibirdMeta[0];
2021-01-07 23:32:19 +00:00
2021-01-02 01:52:44 +00:00
},
created() {
},
data: () => ({
text: 'ASCII',
currentAsciibirdMeta: null,
2021-01-02 01:52:44 +00:00
data: {
message: 'Hello Vue!',
vueCanvas: null,
},
ctx: null,
selectionMode: false,
startPosition: {
x: null,
y: null,
},
}),
computed: {
getFullPath() {
return this.$route.path;
},
2021-01-02 01:52:44 +00:00
generateCanvasId() {
2021-01-07 23:32:19 +00:00
return `canvas${this.currentAsciibirdMeta.key}`;
2021-01-02 01:52:44 +00:00
},
},
watch: {
getFullPath() {
2021-01-02 01:52:44 +00:00
// console.log(this);
this.getData();
},
},
methods: {
2021-01-02 01:52:44 +00:00
dataFieldClass(event) {
this.ctx = event.currentTarget.id;
console.log(this.ctx);
},
getData() {
// Call my lovely API
2021-01-02 01:52:44 +00:00
this.currentAsciibirdMeta = this.$store.state.asciibirdMeta[
this.$route.params.ascii.split('/').join('')
];
2021-01-07 23:32:19 +00:00
2021-01-07 23:53:28 +00:00
console.log({ generateCanvasId: this.generateCanvasId, all_refs: this.$refs, current_canvas_ref: this.$refs[`canvas${this.currentAsciibirdMeta.key-1}`] })
2021-01-07 23:32:19 +00:00
2021-01-07 23:53:28 +00:00
if (this.$refs[`canvas${this.currentAsciibirdMeta.key-1}`]) {
console.log('got', this.$refs[`canvas${this.currentAsciibirdMeta.key-1}`]);
this.ctx = this.$refs[`canvas${this.currentAsciibirdMeta.key-1}`].getContext("2d")
2021-01-07 23:32:19 +00:00
console.log('current ctx', this.ctx)
2021-01-07 23:53:28 +00:00
} else {
console.log("Warning: could not find asciibird meta key " + `canvas${this.currentAsciibirdMeta.key-1}`)
2021-01-07 23:32:19 +00:00
}
2021-01-02 01:52:44 +00:00
},
2021-01-07 23:53:28 +00:00
processMouseDown(e) {
console.log("Mouse down")
this.canvasClass(e)
2021-01-02 01:52:44 +00:00
this.selectionMode = true;
this.startPosition.x = e.clientX;
this.startPosition.y = e.clientY;
},
// dataFieldClass() {
// console.log({'NIGGA': this.$refs[this.generateCanvasId]});
// if (!this.$refs[this.generateCanvasId]) {
// // First render, the element is not there yet
// return null;
// } else {
// // Here is the element
// this.ctx = this.$refs[this.generateCanvasId]
// console.log(this.$refs[this.generateCanvasId]);
// }
// },
2021-01-07 23:53:28 +00:00
processMouseMove(e) {
console.log("Mouse move")
2021-01-02 01:52:44 +00:00
if (this.selectionMode) {
// console.log(this.startPosition);
2021-01-07 23:32:19 +00:00
2021-01-02 01:52:44 +00:00
this.ctx.beginPath();
this.ctx.rect(
this.startPosition.x,
this.startPosition.y,
e.clientX - this.startPosition.x,
e.clientY - this.startPosition.y,
);
this.ctx.closePath();
this.ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
this.ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
this.ctx.strokeStyle = '#f00';
this.ctx.stroke();
}
},
canvasClass(e) {
2021-01-07 23:53:28 +00:00
console.log("Mouse canvasClass")
// this.ctx = e.target;
2021-01-02 01:52:44 +00:00
this.ctx.strokeStyle = 'red';
this.ctx.beginPath();
this.ctx.moveTo(100, 100);
this.ctx.lineTo(200, 100);
this.ctx.lineTo(200, 150);
this.ctx.closePath();
this.ctx.stroke();
},
2021-01-07 23:53:28 +00:00
processMouseUp(e) {
console.log("Mouse up")
2021-01-02 01:52:44 +00:00
this.ctx.fillStyle = '#fff';
this.selectionMode = false;
this.startPosition.x = null;
this.startPosition.y = null;
},
},
2020-12-19 01:27:50 +00:00
};
</script>