-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwojchan.js
63 lines (63 loc) · 4.33 KB
/
wojchan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//wojtekgame 2024
(function wojchan() {
const canvas = document.createElement('canvas');
const grid = 30;
canvas.id = "wojchan"
//for SOME reason the pet sprite is slightly weird and you can see this when you zoom in, so i added +1
//for zIndex, it should be ontop of everything in the site, hopefully.
canvas.width = canvas.height = (grid + 1) * 2;
//STYLING, kinda obscure but it makes sense that this is then used in the minified code >-o
canvas.style = `position:fixed;pointer-events:none;image-rendering:pixelated;z-index:` + Number.MAX_VALUE
//adds itself to the page, opens a context to render simple 2d graphics,
//nothing special if used canvas before,
//and disabling imageSmoothingEnabled should make the canvas no longer blurry when scaled twice (30x30 => 60x60)
//the pet should be at the CENTER of the page (canvas_pos), (mouse) copies the same position.
//YOU CAN CHANGE THE SRC TO ANYTHING AS LONG AS IT FITS IN AN 90x90 SPRITESHEET :D
// (here using the one from the gh repository)
//rend() uses the spritesheet, one grid is 30x30 pixels and this renders one of them.
//then it should present the moving animation
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = false;
let canvas_pos = { x: window.innerWidth / 2, y: window.innerHeight / 2 };
//copy positions, dont go anywhere else
let mouse = { ...canvas_pos };
window.addEventListener('mousemove', (e) => mouse = { x: e.clientX, y: e.clientY });
const image = new Image(grid * 3, grid * 3); //3x3 tiles
//WOJCHAN SPRITE
image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAABaBAMAAADKhlwxAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADr8AAA6/ATgFUyQAAAAYdEVYdFNvZnR3YXJlAFBhaW50Lk5FVCA1LjEuMWK1UgwAAAC2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C"
//image.src = "https://cdn.jsdelivr.net/gh/wojtekcodestoday/wojchan@master/sprite.png";
//THIS IS INCASE YOU WANT TO USE A CUSTOM :D
//image.src = "hazelchan.png";
//basically drawImage is passing (image, x * 30, y * 30, 30, 30, 0, 0, 30, 30)
const rend = (x, y) => { ctx.drawImage(image, x * grid, y * grid, grid, grid, 0, 0, grid, grid) }
let mv = 0;
ctx.scale(2, 2)
//anim() makes the animation render as well as "walking" to the cursor
const anim = () => {
const dx = mouse.x - canvas_pos.x;
const dy = mouse.y - canvas_pos.y;
const dist = Math.sqrt(dx * dx + dy * dy);
const angle = Math.atan2(dy, dx);
const flip = Math.cos(angle) < 0;
ctx.clearRect(0, 0, grid*2, grid*2);
let spd = dist > 150 ? 8 : 4;
canvas.style.left = canvas_pos.x - grid + "px";
canvas.style.top = canvas_pos.y - grid + "px";
//shadow
rend(0, 2);
//walking or idling, (mv) is the walking cycle, it's third frame (4 in total),
//it gets replaced by the idle animation (makes the animation more alive)
rend(dist > 15 ? mv == 3 || mv : 1, flip)
spd == 8 && rend(!flip + 1, 2)
//goes to (speed) at the direction of (angle)
if (dist > 15) {
mv = (mv + 1) % 4;
canvas_pos.x += Math.cos(angle) * spd;
canvas_pos.y += Math.sin(angle) * spd;
}
//redo animation again in 70ms, just so it's not too slow but also not too fast
setTimeout(() => requestAnimationFrame(anim), 70);
}
image.onload = anim;
})();