Skip to content
Merged

Patch #203

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"creative coding"
],
"devDependencies": {
"@litecanvas/jsdom-extras": "^2.0.0",
"@litecanvas/jsdom-extras": "^2.0.1",
"@size-limit/preset-small-lib": "^11.2.0",
"@swc/core": "^1.12.11",
"@swc/core": "^1.13.0",
"@types/jsdom": "^21.1.7",
"ava": "^6.4.1",
"esbuild": "^0.25.6",
Expand Down
27 changes: 15 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default function litecanvas(settings = {}) {
height: null,
autoscale: true,
pixelart: false,
antialias: false,
canvas: null,
global: true,
loop: null,
Expand Down Expand Up @@ -945,7 +944,7 @@ export default function litecanvas(settings = {}) {
DEV: assert(isNumber(volumeFactor), '[litecanvas] sfx() 3rd param must be a number')

if (
root.zzfxV <= 0 ||
!root.zzfxV ||
(navigator.userActivation && !navigator.userActivation.hasBeenActive)
) {
return false
Expand All @@ -972,7 +971,10 @@ export default function litecanvas(settings = {}) {
* @param {number} value
*/
volume(value) {
DEV: assert(isNumber(value), '[litecanvas] volume() 1st param must be a number')
DEV: assert(
isNumber(value) && value >= 0,
'[litecanvas] volume() 1st param must be a positive number or zero'
)

root.zzfxV = value
},
Expand Down Expand Up @@ -1564,7 +1566,7 @@ export default function litecanvas(settings = {}) {

_ctx = _canvas.getContext('2d')

on(_canvas, 'click', () => root.focus())
on(_canvas, 'click', () => focus())

/** @ts-ignore */
_canvas.style = ''
Expand Down Expand Up @@ -1592,8 +1594,8 @@ export default function litecanvas(settings = {}) {
'[litecanvas] litecanvas() option "width" is required when the option "height" is defined'
)

const width = settings.width || root.innerWidth,
height = settings.height || settings.width || root.innerHeight
const width = settings.width > 0 ? settings.width : innerWidth,
height = settings.width > 0 ? settings.height || settings.width : innerHeight

instance.def('W', width)
instance.def('H', height)
Expand All @@ -1608,29 +1610,30 @@ export default function litecanvas(settings = {}) {
_canvas.style.margin = 'auto'
}

_scale = math.min(root.innerWidth / width, root.innerHeight / height)
_scale = math.min(innerWidth / width, innerHeight / height)
_scale = maxScale > 1 && _scale > maxScale ? maxScale : _scale
_scale = (settings.pixelart ? ~~_scale : _scale) || 1

_canvas.style.width = width * _scale + 'px'
_canvas.style.height = height * _scale + 'px'
}

// restore canvas image rendering properties
if (!settings.antialias || settings.pixelart) {
// set canvas image rendering properties
if (settings.pixelart) {
_ctx.imageSmoothingEnabled = false
_canvas.style.imageRendering = 'pixelated'
}

// reset the default text align and baseline
// set the default text align and baseline
instance.textalign('start', 'top')

// trigger "resized" event
// note: not triggered before the "init" event
instance.emit('resized', _scale)

// paint a temporary background
instance.cls(0)

// force redraw
// force redraw when the canvas is not animated
if (!settings.animate) {
raf(drawFrame)
}
Expand Down
9 changes: 4 additions & 5 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,19 +594,18 @@ type LitecanvasOptions = {
canvas?: HTMLCanvasElement | string
/**
* If `true` (default) scales the canvas to fill the screen, but preserving the aspect ratio.
* Instead of `true`, you can pass a number to limit the maximum scale factor of the canvas.
* Instead of `true`, you can pass a number to determine the maximum scale.
*
* Note: Only works if the option "width" was specified.
*/
autoscale?: boolean | number
/**
* If `true`, the pixel art images won't look blurry.
* Also, disables canvas built-in antialias.
*
* Default: `false`
*/
pixelart?: boolean
/**
* If `false` (default), disable the canvas antialias.
*/
antialias?: boolean
/**
* If `true` (default), all methods and properties of the engine will be exposed to the global scope (window).
*/
Expand Down