|
| 1 | +import { Background } from './Background.js' |
| 2 | + |
| 3 | +const canvas = document.getElementById('background-grid') |
| 4 | +const ctx = canvas.getContext('2d') |
| 5 | + |
| 6 | +const background = new Background(canvas, 50, 10) |
| 7 | +background.style.grid.lineWidth = 1 |
| 8 | +background.style.grid.color = '#2d333b' |
| 9 | + |
| 10 | +// resize page |
| 11 | +window.addEventListener('resize', () => { |
| 12 | + background.resize(window.innerWidth, window.innerHeight) |
| 13 | +}) |
| 14 | +background.resize(window.innerWidth, window.innerHeight) |
| 15 | + |
| 16 | +// move background grid |
| 17 | +canvas.addEventListener('mousemove', event => { |
| 18 | + |
| 19 | + // only move if left mouse button is pressed |
| 20 | + if (event.buttons == 1) { |
| 21 | + background.moveBy(event.movementX, event.movementY) |
| 22 | + } |
| 23 | +}) |
| 24 | + |
| 25 | +// zoom in and out |
| 26 | +canvas.addEventListener('wheel', event => { |
| 27 | + |
| 28 | + // zoom in |
| 29 | + if (event.deltaY < 0) { |
| 30 | + background.zoomAt(event.clientX, event.clientY, true) |
| 31 | + } |
| 32 | + |
| 33 | + // zoom out |
| 34 | + else if (event.deltaY > 0) { |
| 35 | + background.zoomAt(event.clientX, event.clientY, false) |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +// move origin to center of page |
| 40 | +background.moveTo(canvas.width / 2, canvas.height / 2) |
| 41 | + |
| 42 | +background.update(() => { |
| 43 | + ctx.fillStyle = '#c93c37' |
| 44 | + ctx.strokeStyle = '#539bf5' |
| 45 | + ctx.beginPath() |
| 46 | + ctx.moveTo(window.innerWidth / 2, window.innerHeight / 2); |
| 47 | + ctx.lineTo(background.getOrigin().posX, background.getOrigin().posY) |
| 48 | + ctx.stroke() |
| 49 | + ctx.closePath() |
| 50 | + ctx.beginPath() |
| 51 | + ctx.arc(background.getOrigin().posX, background.getOrigin().posY, 5, 0, 2 * Math.PI); |
| 52 | + ctx.fill() |
| 53 | + ctx.closePath() |
| 54 | + |
| 55 | +}) |
0 commit comments