Resource
Resource library, external resource management (static class).
Used to manage and orderly load assets such as images, videos, sounds, etc.
Key Properties
task: TaskProcessor
Global parallel asynchronous resource loading tasks, automatically limits the maximum concurrency, see example.
queue: TaskProcessor
Global sequential task queue (currently mainly used for pattern generation), prevents blocking main thread rendering, see example.
Key Methods
loadImage ( key: string, format?: IExportFileType ): Promise<ILeaferImage>
Preload image resources. key is the image URL, and format is used to force the image type.
setImage ( key: string, value: string | IObject | ILeaferImage | ILeaferCanvas, format?: IExportFileType ): ILeaferImage
Customize the key of image resources. It is recommended to use the leafer:// protocol + file extension for easy identification when exporting resources.
value supports base64, raw images, canvas, and wrapped cross-platform images and canvas.
format is used to force the image type.
set ( key: string, value: any )
Set the resource key. Image types must be ILeaferImage.
get ( key: string ): any
Get the resource object by key.
remove ( key: string )
Remove the resource by key. Memory will be automatically released after removal.
destroy ( )
Destroy all resources.
Examples
Wait for the image to load, then add it to the engine
Convert raw image object to URL
// #图片 url [原始图片对象转 url]
import { Leafer, Image, Platform, Resource } from 'leafer-ui'
const leafer = new Leafer({ view: window })
Platform.origin.loadImage('/image/leafer.jpg').then((img) => { // 加载原始图片对象(跨平台)
const { url } = Resource.setImage('leafer://test1.jpg', img) // 原始图片对象 转为 自定义资源符
leafer.add(new Image({ url })) // url = leafer://test1.jpg
})Convert raw canvas object to URL
// #图片 url [原始 canvas 对象转 url]
import { Leafer, Image, Platform, Resource } from 'leafer-ui'
const leafer = new Leafer({ view: window })
Platform.origin.loadImage('/image/leafer.jpg').then((img) => {
const canvas = document.createElement('canvas') // 原始画布
canvas.width = img.width
canvas.height = img.height
canvas.getContext('2d').drawImage(img, 0, 0)
const { url } = Resource.setImage('leafer://test2.jpg', canvas) // 原始canvas 对象 转为 自定义资源符
leafer.add(new Image({ url })) // url = leafer://test2.jpg
})Convert cross-platform LeaferCanvas object to URL
// #图片 url [跨平台 LeaferCanvas 对象转 url]
import { Leafer, Image, LeaferCanvas, Platform, Resource } from 'leafer-ui'
const leafer = new Leafer({ view: window })
Platform.origin.loadImage('/image/leafer.jpg').then((img) => {
const leaferCanvas = new LeaferCanvas({ width: img.width, height: img.height }) // LeaferCanvas 跨平台画布
leaferCanvas.drawImage(img, 0, 0)
const { url } = Resource.setImage('leafer://test3.jpg', leaferCanvas) // LeaferCanvas 转为 自定义资源符
leafer.add(new Image({ url })) // url = leafer://test3.jpg
})Add a custom asynchronous task
export() will wait for this task to complete before exporting the image.