Skip to content

Commit

Permalink
add screenshake function to renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryMoon committed Feb 13, 2025
1 parent c94c1e6 commit 0e2bf43
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/internal/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
const scale_times = 0.4; // Multiplicative scaling for bloom.
let canvasImageData = null; // Stores image data for the canvas.

// Effects.
let screenshakeDuration = 0;


/**
* Initialization function that precomputes bloom and scanline ranges.
*
Expand All @@ -37,7 +41,7 @@
phosphor_bloom[ i ] = ( scale_times * ( i / 255 ) ** ( 1 / 2.2 ) ) + scale_add;
}

};
}


/**
Expand All @@ -53,14 +57,25 @@

beep8.Core.realCtx.imageSmoothingEnabled = false;

// Canvas Drawing location.
let x = 0;
let y = 0;

// Do screenshake.
if ( screenshakeDuration > 0 ) {
x = Math.min( 10, Math.round( ( Math.random() * screenshakeDuration ) - ( screenshakeDuration / 2 ) ) );
y = Math.min( 10, Math.round( ( Math.random() * screenshakeDuration ) - ( screenshakeDuration / 2 ) ) );
screenshakeDuration -= 1;
}

beep8.Core.realCtx.clearRect(
0, 0,
beep8.Core.realCanvas.width, beep8.Core.realCanvas.height
);

beep8.Core.realCtx.drawImage(
beep8.Core.canvas,
0, 0,
x, y,
beep8.Core.realCanvas.width, beep8.Core.realCanvas.height
);

Expand All @@ -77,6 +92,19 @@
}


/**
* Triggers the screenshake effect.
*
* @param {number} duration - The duration of the screenshake effect in ms.
* @returns {void}
*/
beep8.Renderer.shakeScreen = function( duration ) {

screenshakeDuration = duration;

}


/**
* Marks the screen as dirty, so it will be re-rendered on the next frame.
*
Expand Down

0 comments on commit 0e2bf43

Please sign in to comment.