| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- export default class CanvasOperator {
- constructor(canvas) {
- this.canvas = canvas
- }
- /* ---------- RAF 封装 ---------- */
- raf(cb) {
- if (typeof this.canvas.requestAnimationFrame === 'function') {
- return this.canvas.requestAnimationFrame(cb)
- }
- if (typeof requestAnimationFrame === 'function') {
- return requestAnimationFrame(cb)
- }
- return setTimeout(cb, 16)
- }
- cancelRaf(id) {
- if (typeof this.canvas.cancelAnimationFrame === 'function') {
- return this.canvas.cancelAnimationFrame(id)
- }
- if (typeof cancelAnimationFrame === 'function') {
- return cancelAnimationFrame(id)
- }
- return clearTimeout(id)
- }
- /* ---------- 图片加载 ---------- */
- async loadImage(filePath) {
- let img
- if (typeof this.canvas.createImage === 'function') {
- img = this.canvas.createImage()
- }
- if (typeof Image !== 'undefined') {
- img = new Image()
- }
- // #ifndef H5
- img.crossOrigin = 'anonymous'
- // #endif
- return new Promise((resolve, reject) => {
- img.src = filePath
- img.onload = () => resolve(img)
- img.onerror = () => {
- console.error('[loadImage] 图片加载失败:', filePath, '请检查图片路径')
- reject(new Error('图片加载失败'))
- }
- })
- }
- }
|