@@ -728,19 +728,46 @@ export default function litecanvas(settings = {}) {
728728 _ctx . drawImage ( source , ~ ~ x , ~ ~ y )
729729 } ,
730730
731+ /**
732+ * Draw a sprite pxiel by pixel represented by a string. Each pixel must be a base 36 number (0-9 or a-z) or a dot.
733+ *
734+ * @param {number } x
735+ * @param {number } y
736+ * @param {number } width
737+ * @param {number } height
738+ * @param {string } pixels
739+ */
740+ spr ( x , y , width , height , pixels ) {
741+ DEV: assert ( isNumber ( x ) , '[litecanvas] spr() 1st param must be a number' )
742+ DEV: assert ( isNumber ( y ) , '[litecanvas] spr() 2nd param must be a number' )
743+ DEV: assert ( isNumber ( width ) , '[litecanvas] spr() 3rd param must be a number' )
744+ DEV: assert ( isNumber ( height ) , '[litecanvas] spr() 4th param must be a number' )
745+ DEV: assert ( 'string' === typeof pixels , '[litecanvas] spr() 5th param must be a string' )
746+
747+ const chars = pixels . replace ( / \s / g, '' )
748+ for ( let gridx = 0 ; gridx < width ; gridx ++ ) {
749+ for ( let gridy = 0 ; gridy < height ; gridy ++ ) {
750+ const char = chars [ height * gridy + gridx ] || '.'
751+ if ( char !== '.' ) {
752+ instance . rectfill ( x + gridx , y + gridy , 1 , 1 , parseInt ( char , 16 ) || 0 )
753+ }
754+ }
755+ }
756+ } ,
757+
731758 /**
732759 * Draw in an OffscreenCanvas and returns its image.
733760 *
734761 * @param {number } width
735762 * @param {number } height
736- * @param {string[]| drawCallback } drawing
763+ * @param {drawCallback } callback
737764 * @param {object } [options]
738765 * @param {number } [options.scale=1]
739766 * @param {OffscreenCanvas } [options.canvas]
740767 * @returns {ImageBitmap }
741768 * @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
742769 */
743- paint ( width , height , drawing , options = { } ) {
770+ paint ( width , height , callback , options = { } ) {
744771 DEV: assert (
745772 isNumber ( width ) && width >= 1 ,
746773 '[litecanvas] paint() 1st param must be a positive number'
@@ -750,7 +777,7 @@ export default function litecanvas(settings = {}) {
750777 '[litecanvas] paint() 2nd param must be a positive number'
751778 )
752779 DEV: assert (
753- 'function' === typeof drawing || Array . isArray ( drawing ) ,
780+ 'function' === typeof callback ,
754781 '[litecanvas] paint() 3rd param must be a function or array'
755782 )
756783 DEV: assert (
@@ -765,37 +792,16 @@ export default function litecanvas(settings = {}) {
765792 const /** @type {OffscreenCanvas } */
766793 canvas = options . canvas || new OffscreenCanvas ( 1 , 1 ) ,
767794 scale = options . scale || 1 ,
768- contextOriginal = _ctx
795+ currentContext = _ctx // context backup
769796
770797 canvas . width = width * scale
771798 canvas . height = height * scale
772799
773800 _ctx = canvas . getContext ( '2d' )
774801 _ctx . scale ( scale , scale )
802+ callback ( _ctx )
775803
776- // draw pixel art if `draw` is a array
777- if ( Array . isArray ( drawing ) ) {
778- let x = 0 ,
779- y = 0
780-
781- _ctx . imageSmoothingEnabled = false
782-
783- for ( const str of drawing ) {
784- for ( const color of str ) {
785- if ( ' ' !== color && '.' !== color ) {
786- // support for 16-color palette using hex (from 0 to f)
787- instance . rectfill ( x , y , 1 , 1 , parseInt ( color , 16 ) )
788- }
789- x ++
790- }
791- y ++
792- x = 0
793- }
794- } else {
795- drawing ( _ctx )
796- }
797-
798- _ctx = contextOriginal // restore the context
804+ _ctx = currentContext // restore the context
799805
800806 return canvas . transferToImageBitmap ( )
801807 } ,
0 commit comments