Skip to content
Snippets Groups Projects
Commit d14845f9 authored by Adam Procter's avatar Adam Procter
Browse files

tidy up

this included using vue data to render the canvas 2d elements.
parent 8c3a3309
No related branches found
No related tags found
No related merge requests found
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
<div class="hello"> <div class="hello">
<h1>{{ msg }}</h1> <h1>{{ msg }}</h1>
<!-- <canvas ref="canvas" :width="windowWidth" :height="windowHeight"></canvas> --> <canvas ref="canvas"></canvas>
<canvas ref="canvas" width="150" height="150"></canvas>
<div class="controls"> <div class="controls">
<div class="btn-row"> <div class="btn-row">
...@@ -21,6 +20,8 @@ ...@@ -21,6 +20,8 @@
</template> </template>
<script> <script>
var canvas = null
export default { export default {
name: 'HelloWorld', name: 'HelloWorld',
props: { props: {
...@@ -29,152 +30,66 @@ export default { ...@@ -29,152 +30,66 @@ export default {
data: function() { data: function() {
return { return {
configRect: { configRect: {
x: 10, x: -25,
y: 30, y: -25,
height: 50, height: 50,
width: 50, width: 50,
fill: 'rgb(200, 0, 0)' fill: 'rgb(200, 0, 0)'
},
configHandle: {
x: 25,
y: 25,
height: 4,
width: 4,
fill: 'black'
} }
} }
}, },
mounted() { mounted() {
// listen for mouse events canvas = this.$refs.canvas
// this.ctx.onmousedown = myDown this.ctx = canvas.getContext('2d')
// this.ctx.onmouseup = myUp this.draw()
// this.ctx.onmousemove = myMove },
methods: {
this.ctx = this.$refs.canvas.getContext('2d') draw() {
// this.draw() this.box(this.ctx, this.x, this.y)
this.ctx.fillStyle = 'rgb(200, 0, 0)' console.log('draw')
this.ctx.fillRect(this.x, this.y, this.height, this.width) },
this.ctx.fillStyle = 'rgba(0, 0, 200, 0.5)' box(ctx, x, y) {
this.ctx.fillRect(30, 30, 50, 50) ctx.setTransform(1, 0, 0, 1, x, y)
// this.ctx.font = '48px serif' ctx.fillStyle = this.configRect.fill
// this.ctx.fillText('Hello world', 10, 50) ctx.fillRect(
this.configRect.x,
this.configRect.y,
this.configRect.height,
this.configRect.width
)
ctx.fillStyle = this.configHandle.fill
ctx.fillRect(
this.configHandle.x,
this.configHandle.y,
this.configHandle.height,
this.configHandle.width
)
ctx.stroke()
ctx.setTransform(1, 0, 0, 1, 0, 0)
},
clear(ctx) {
ctx.clearRect(0, 0, canvas.width, canvas.height)
}
} }
// methods: {
// draw() {
// this.clear()
// // redraw each shape in the shapes[] array
// for (var i = 0; i < this.shapes.length; i++) {
// // decide if the shape is a rect or circle
// // (it's a rect if it has a width property)
// if (this.shapes[i].width) {
// rect(this.shapes[i])
// } else {
// circle(this.shapes[i])
// }
// }
// },
// clear() {
// this.ctx.clearRect(0, 0, WIDTH, HEIGHT)
// }
// }
} }
// drag related variables
// var dragok = false
// var startX
// var startY
// handle mousedown events
// function myDown(e) {
// // tell the browser we're handling this mouse event
// e.preventDefault()
// e.stopPropagation()
// // get the current mouse position
// var mx = parseInt(e.clientX - offsetX)
// var my = parseInt(e.clientY - offsetY)
// // test each shape to see if mouse is inside
// dragok = false
// for (var i = 0; i < shapes.length; i++) {
// var s = shapes[i]
// // decide if the shape is a rect or circle
// if (s.width) {
// // test if the mouse is inside this rect
// if (mx > s.x && mx < s.x + s.width && my > s.y && my < s.y + s.height) {
// // if yes, set that rects isDragging=true
// dragok = true
// s.isDragging = true
// }
// } else {
// var dx = s.x - mx
// var dy = s.y - my
// // test if the mouse is inside this circle
// if (dx * dx + dy * dy < s.r * s.r) {
// dragok = true
// s.isDragging = true
// }
// }
// }
// // save the current mouse position
// startX = mx
// startY = my
// }
// // handle mouseup events
// function myUp(e) {
// // tell the browser we're handling this mouse event
// e.preventDefault()
// e.stopPropagation()
// // clear all the dragging flags
// dragok = false
// for (var i = 0; i < shapes.length; i++) {
// shapes[i].isDragging = false
// }
// }
// // handle mouse moves
// function myMove(e) {
// // if we're dragging anything...
// if (dragok) {
// // tell the browser we're handling this mouse event
// e.preventDefault()
// e.stopPropagation()
// // get the current mouse position
// var mx = parseInt(e.clientX - offsetX)
// var my = parseInt(e.clientY - offsetY)
// // calculate the distance the mouse has moved
// // since the last mousemove
// var dx = mx - startX
// var dy = my - startY
// // move each rect that isDragging
// // by the distance the mouse has moved
// // since the last mousemove
// for (var i = 0; i < shapes.length; i++) {
// var s = shapes[i]
// if (s.isDragging) {
// s.x += dx
// s.y += dy
// }
// }
// // redraw the scene with the new rect positions
// draw()
// // reset the starting mouse position for the next mousemove
// startX = mx
// startY = my
// }
// }
//
</script> </script>
<!-- Add "scoped" attribute to limit CSS to this component only --> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> <style scoped>
canvas { canvas {
border: 1px solid black; border: 1px solid black;
/* width: 100%; */
/* height: 150px;
background-color: white; */
} }
.controls { .controls {
......
import Vue from "vue"; import Vue from 'vue'
import Vuex from "vuex"; import Vuex from 'vuex'
Vue.use(Vuex); Vue.use(Vuex)
export default new Vuex.Store({ export default new Vuex.Store({
state: {}, state: {},
mutations: {}, mutations: {},
actions: {}, actions: {},
modules: {} modules: {}
}); })
<template> <template>
<div class="about"> <div class="about">
<h1>This is an about page</h1> <h1>About</h1>
<p>Mucking about for nodenogg.in</p>
</div> </div>
</template> </template>
<template> <template>
<div class="home"> <div class="home">
<HelloWorld msg="Welcome to Vue Canvas &amp; testing ground" /> <HelloWorld msg="Welcome to Vue &amp; Canvas testing ground" />
</div> </div>
</template> </template>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment