From 34e61adbe8c8ab736c6850aa8d99a53907441fda Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Fri, 3 Jan 2020 15:16:22 +0100 Subject: [PATCH] Add mouse listeners --- src/components.js/Canvas.js | 22 +++++++++- src/lib/index.js | 88 ++++++++++++++++++++++++------------- 2 files changed, 77 insertions(+), 33 deletions(-) diff --git a/src/components.js/Canvas.js b/src/components.js/Canvas.js index 2cfb654..e446377 100644 --- a/src/components.js/Canvas.js +++ b/src/components.js/Canvas.js @@ -1,5 +1,14 @@ import React, { useLayoutEffect, useRef } from 'react' -import { main } from '../lib' +import { + main, + mousedown, + mousemove, + mouseup, + click, + dblclick, + mouseenter, + mouseleave +} from '../lib' import styles from './Canvas.css' @@ -7,7 +16,16 @@ export const Canvas = () => { const canvasRef = useRef(null) useLayoutEffect(() => { - main(canvasRef.current) + const canvas = canvasRef.current + main(canvas) + + canvas.addEventListener('mousedown', mousedown) + canvas.addEventListener('mouseup', mouseup) + canvas.addEventListener('mousemove', mousemove) + canvas.addEventListener('click', click) + canvas.addEventListener('dblclick', dblclick) + canvas.addEventListener('mouseenter', mouseenter) + canvas.addEventListener('mouseout', mouseleave) }) return ( diff --git a/src/lib/index.js b/src/lib/index.js index 9beaa35..889bc21 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -3,12 +3,12 @@ console.log('import entrypoint lib') let global_scale = 1 let global_offset = [0, 0] -const cos_60 = Math.cos(60 * Math.PI / 180) -const sin_60 = Math.sin(60 * Math.PI / 180) +const cos_60 = Math.cos((60 * Math.PI) / 180) +const sin_60 = Math.sin((60 * Math.PI) / 180) const TileType = { - LAND : 0, - WATER : 1 + LAND: 0, + WATER: 1 } class TileGrid { @@ -16,8 +16,8 @@ class TileGrid { this.width = 100 this.height = 100 this.tiles = new Array(this.width * this.height) - for (let y=0; y 15) { ctx.save() - ctx.translate(coord[0], coord[1]) - ctx.scale(1/global_scale, 1/global_scale) - ctx.font = "12px Arial" - ctx.fillStyle = "black" - ctx.textAlign = "center" + ctx.translate(coord[0], coord[1]) + ctx.scale(1 / global_scale, 1 / global_scale) + ctx.font = '12px Arial' + ctx.fillStyle = 'black' + ctx.textAlign = 'center' ctx.fillText(String([x, y]), 0, 0) ctx.restore() } @@ -119,8 +116,8 @@ function draw_tower(ctx, index) { ctx.lineWidth = 1 / global_scale ctx.translate(coord[0], coord[1]) - ctx.fillStyle = "gray" - ctx.strokeStyle = "black" + ctx.fillStyle = 'gray' + ctx.strokeStyle = 'black' ctx.beginPath() ctx.arc(0, 0, radius_1, 0, Math.PI) ctx.lineTo(-radius_2, -height_1) @@ -129,8 +126,8 @@ function draw_tower(ctx, index) { ctx.fill() ctx.stroke() - ctx.fillStyle = "darkred" - ctx.strokeStyle = "black" + ctx.fillStyle = 'darkred' + ctx.strokeStyle = 'black' ctx.beginPath() ctx.arc(0, -height_1, radius_2, Math.PI, 0, true) ctx.lineTo(0, -height_2) @@ -150,8 +147,15 @@ function draw(ctx, offset, scale) { draw_map(ctx) - let towers = [[2, 0], [2, 1], [3, 1], [2, 3], [3, 3], [2, 4]] - for (let i=0; i<6; i++) { + let towers = [ + [2, 0], + [2, 1], + [3, 1], + [2, 3], + [3, 3], + [2, 4] + ] + for (let i = 0; i < 6; i++) { draw_tower(ctx, towers[i]) } } @@ -164,3 +168,25 @@ export function main(canvas) { draw(ctx, [100, 100], 10) } } + +export function mousedown(evt) { + console.log(evt) +} +export function mouseup(evt) { + console.log(evt) +} +export function mousemove(evt) { + // console.log(evt) +} +export function click(evt) { + console.log(evt) +} +export function dblclick(evt) { + console.log(evt) +} +export function mouseenter(evt) { + console.log(evt) +} +export function mouseleave(evt) { + console.log(evt) +}