Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leafer的Canvas元素改变宽高后,获取imageData很慢 #292

Closed
bgyh2003 opened this issue Dec 17, 2024 · 2 comments
Closed

leafer的Canvas元素改变宽高后,获取imageData很慢 #292

bgyh2003 opened this issue Dec 17, 2024 · 2 comments

Comments

@bgyh2003
Copy link

bgyh2003 commented Dec 17, 2024

leafer的Canvas元素改变宽高后,获取imageData很慢,要100多毫秒

const { Canvas } = LeaferUI
const canvas = new Canvas({ width: 10000, height: 10000 })

canvas.width = 700
canvas.height = 300

console.time("getImageData")
const imageData = canvas.context.getImageData(0, 0, 700, 300)
console.timeEnd("getImageData") // getImageData: 102.496826171875 ms

原生canvas只需要4毫秒

const canvas = document.createElement("canvas")
canvas.width = 10000
canvas.height = 10000
const ctx = canvas.getContext("2d")

canvas.width = 700
canvas.height = 300

console.time("getImageData")
const imageData = ctx.getImageData(0, 0, 700, 300)
console.timeEnd("getImageData") // getImageData: 3.77783203125 ms

延迟一秒获取,速度就恢复正常了,5-10毫秒,这是为什么呢

const { Canvas } = LeaferUI
const canvas = new Canvas({ width: 10000, height: 10000 })

canvas.width = 700
canvas.height = 300

setTimeout(() => {
  console.time("getImageData")
  const imageData = canvas.context.getImageData(0, 0, 700, 300)
  console.timeEnd("getImageData") // getImageData: 10.425048828125 ms
}, 1000)

因为我需要处理canvas的像素,可能要不断的getImageData,100多毫秒很慢啊,怎样解决呢

@leaferjs
Copy link
Owner

Canvas 增加 contextSettings.willReadFrequently 配置试试

https://www.leaferjs.com/ui/reference/display/Canvas.html#contextsettings-icanvasrenderingcontext2dsettings

@bgyh2003
Copy link
Author

Canvas 增加 contextSettings.willReadFrequently 配置试试

https://www.leaferjs.com/ui/reference/display/Canvas.html#contextsettings-icanvasrenderingcontext2dsettings

不好使,已经用别的方法解决了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants