|
| 1 | +function distanceBetween(point1, point2) { |
| 2 | + return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2)); |
| 3 | +} |
| 4 | +function angleBetween(point1, point2) { |
| 5 | + return Math.atan2( point2.x - point1.x, point2.y - point1.y ); |
| 6 | +} |
| 7 | + |
| 8 | +var isDrawing, lastPoint; |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +// var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); |
| 13 | +var game = new Phaser.Game(800, 600, Phaser.WEBGL, 'phaser-example', { preload: preload, create: create, update: update, render: render }); |
| 14 | + |
| 15 | +function preload() { |
| 16 | + |
| 17 | + game.load.image('mushroom', 'assets/sprites/chunk.png'); |
| 18 | + |
| 19 | +} |
| 20 | + |
| 21 | +var mushroom; |
| 22 | +var texture; |
| 23 | +var image; |
| 24 | + |
| 25 | +var down; |
| 26 | +var p; |
| 27 | + |
| 28 | +function create() { |
| 29 | + |
| 30 | + texture = game.add.renderTexture(800, 600, 'mousetrail', true); |
| 31 | + |
| 32 | + // We create a sprite (rather than using the factory) so it doesn't get added to the display, as we only need its texture data. |
| 33 | + mushroom = new Phaser.Sprite(game, 0, 0, 'mushroom'); |
| 34 | + mushroom.anchor.setTo(0.5, 0.5); |
| 35 | + |
| 36 | + // This is the sprite that is drawn to the display. We've given it the renderTexture as its texture. |
| 37 | + image = game.add.image(0, 0, texture); |
| 38 | + |
| 39 | + domElement = document.getElementById('phaser-example'); |
| 40 | + |
| 41 | + p = new Phaser.Point(); |
| 42 | + |
| 43 | + // domElement.addEventListener('mousemove', onMouseMove, true); |
| 44 | + // domElement.addEventListener('mousedown', onMouseDown, true); |
| 45 | + // // domElement.addEventListener('mouseout', onMouseOut, true); |
| 46 | + // domElement.addEventListener('mouseup', onMouseUp, true); |
| 47 | + |
| 48 | + game.input.mouse.mouseDownCallback = onMouseDown; |
| 49 | + game.input.mouse.mouseUpCallback = onMouseUp; |
| 50 | + game.input.mouse.mouseMoveCallback = onMouseMove; |
| 51 | + |
| 52 | + texture.render(mushroom, p, false); |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +function onMouseDown(e) { |
| 57 | + isDrawing = true; |
| 58 | + lastPoint = { x: e.clientX, y: e.clientY }; |
| 59 | +} |
| 60 | + |
| 61 | +function onMouseUp(e) { |
| 62 | + isDrawing = false; |
| 63 | +} |
| 64 | + |
| 65 | +function onMouseMove(e) { |
| 66 | + |
| 67 | + if (!isDrawing) return; |
| 68 | + |
| 69 | + var currentPoint = { x: e.clientX, y: e.clientY }; |
| 70 | + var dist = distanceBetween(lastPoint, currentPoint); |
| 71 | + var angle = angleBetween(lastPoint, currentPoint); |
| 72 | + |
| 73 | + for (var i = 0; i < dist; i+=5) { |
| 74 | + x = lastPoint.x + (Math.sin(angle) * i) - 25; |
| 75 | + y = lastPoint.y + (Math.cos(angle) * i) - 25; |
| 76 | + p.set(x, y); |
| 77 | + texture.render(mushroom, p, false); |
| 78 | + |
| 79 | + // ctx.beginPath(); |
| 80 | + // ctx.arc(x+10, y+10, 20, false, Math.PI * 2, false); |
| 81 | + // ctx.closePath(); |
| 82 | + // ctx.fill(); |
| 83 | + // ctx.stroke(); |
| 84 | + } |
| 85 | + |
| 86 | + lastPoint = currentPoint; |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +function tint() { |
| 91 | + |
| 92 | + image.tint = Math.random() * 0xFFFFFF; |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +function update() { |
| 97 | + |
| 98 | + // if (down) |
| 99 | + // { |
| 100 | + // } |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +function render() { |
| 105 | + |
| 106 | +} |
0 commit comments