-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
117cbbb
commit 40fdf7b
Showing
8 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import saw from './assets/img/saw.png'; | ||
import { Sprite } from 'kontra'; | ||
import { BACK_FORTH, UP_DOWN } from './sawBehavior'; | ||
|
||
export class Saw { | ||
x; | ||
y; | ||
orgX; | ||
orgY; | ||
sprite = { render: () => {} }; | ||
distance = 100; | ||
speed = 1; | ||
|
||
constructor(x, y, { behavior, distance }) { | ||
this.x = x; | ||
this.y = y; | ||
this.orgX = x; | ||
this.orgY = y; | ||
this.distance = distance; | ||
this.behavior = behavior; | ||
this.createSprite(); | ||
} | ||
|
||
update() { | ||
this.moveDistance(this.behavior, this.distance); | ||
this.sprite.rotation += 5; | ||
} | ||
moveDistance(behavior, distance) { | ||
let axis = 'x'; | ||
switch (behavior) { | ||
case UP_DOWN: | ||
axis = 'y'; | ||
break; | ||
case BACK_FORTH: | ||
axis = 'x'; | ||
break; | ||
default: | ||
axis = 'x'; | ||
} | ||
|
||
this[axis] += this.speed; | ||
this.sprite.x = this.x; | ||
this.sprite.y = this.y; | ||
} | ||
render(_ctx) { | ||
this.sprite.render(); | ||
} | ||
createSprite() { | ||
const image = new Image(); | ||
image.src = saw; | ||
image.onerror = function (err) { | ||
console.log(err); | ||
}; | ||
image.onload = () => { | ||
this.sprite = Sprite({ | ||
x: this.x, | ||
y: this.y, | ||
anchor: { x: 0.5, y: 0.5 }, | ||
image: image, | ||
scaleX: 2, | ||
scaleY: 2, | ||
}); | ||
}; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const BACK_FORTH = 0; | ||
export const UP_DOWN = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
import { Vector } from 'kontra'; | ||
|
||
export const scaleToFit = () => { | ||
// TODO (johnedvard) listen for window resize, debounce, and scale to fit mac height and width whilst keeping aspect ratio | ||
}; | ||
export const lineIntersection = (p1, p2, p3, p4) => { | ||
const d = (p2.x - p1.x) * (p4.y - p3.y) - (p2.y - p1.y) * (p4.x - p3.x); | ||
if (d == 0) return null; // parallel lines | ||
const u = ((p3.x - p1.x) * (p4.y - p3.y) - (p3.y - p1.y) * (p4.x - p3.x)) / d; | ||
const v = ((p3.x - p1.x) * (p2.y - p1.y) - (p3.y - p1.y) * (p2.x - p1.x)) / d; | ||
if (u < 0.0 || u > 1.0) return null; // intersection point not between p1 and p2 | ||
if (v < 0.0 || v > 1.0) return null; // intersection point not between p3 and p4 | ||
const intersectionX = p1.x + u * (p2.x - p1.x); | ||
const intersectionY = p1.y + u * (p2.y - p1.y); | ||
let intersection = Vector(intersectionX, intersectionY); | ||
return intersection; | ||
}; |