canvas-capture icon indicating copy to clipboard operation
canvas-capture copied to clipboard

Why import { CanvasCapture } from 'canvas-capture' cause cpu usage 99%?

Open wxfred opened this issue 2 years ago • 8 comments

I‘m using electron to develop vue app, and only import this package with import { CanvasCapture } from 'canvas-capture' the Chrome Performance monitor shows the cpu usage is up to 99%! After commented out the import, the cpu usage is lower than 10%.

wxfred avatar Aug 01 '22 11:08 wxfred

Hey, thanks for the issue. Can you be a little more specific, which performance monitor in chrome? The built-in one in the devtools? can you post a screenshot?

Nothing really "happens" in my codebase when the import occurs, so I wonder if this is coming from a dependency. Do you have any insight into what functions are being called that are hogging the CPU?

amandaghassaei avatar Aug 01 '22 20:08 amandaghassaei

Hey, thanks for the issue. Can you be a little more specific, which performance monitor in chrome? The built-in one in the devtools? can you post a screenshot?

Nothing really "happens" in my codebase when the import occurs, so I wonder if this is coming from a dependency. Do you have any insight into what functions are being called that are hogging the CPU?

Yes, the built-in one in the devtools. Thanks for replay, I will create another empty project to test.

wxfred avatar Aug 04 '22 09:08 wxfred

Steps to reproduce the problem: 1.Install vue-cli, create a test project

npm install -g @vue/cli
vue create canvascapture
cd ./canvascapture

2.Add electron dev tool, install canvas-capture, and run

vue add electron-builder
npm install canvas-capture --save
npm run electron:serve

3.Press F12 to open Chrome Dev Tools in your electron app, check the Performance monitor, the CPU usage is low currently

4.Import CanvasCapture in Home.vue, save the change, then check the monitor again, the CPU usage is up to 99%

...
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
import { CanvasCapture } from 'canvas-capture'
console.log(CanvasCapture)

export default {
...

By the way, the final product (npm run electron:build) has no weird cpu high usage. Maybe something in electron dev mode are conflict with canvas-capture.

wxfred avatar Sep 03 '22 06:09 wxfred

thanks for this info. will take a look when I get the chance. good to know it's not affecting the final build!

amandaghassaei avatar Sep 05 '22 02:09 amandaghassaei

I found that, if I require CanvasCapture in mounted, the CPU usage will be fine.

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
//import { CanvasCapture } from 'canvas-capture' // not work
//const { CanvasCapture } = require('canvas-capture') // not work, wrapping with setTimeOut is the same
let CanvasCapture

export default {
  mounted() {
    const CC = require('canvas-capture') // it works
    CanvasCapture = CC.CanvasCapture
  }
}

It is weird, but seems to be a work around.

wxfred avatar Jan 05 '23 02:01 wxfred

thanks for the update on this @wxfred !

amandaghassaei avatar Jan 12 '23 23:01 amandaghassaei

Same problem here, the whole browser is lagging (Chrome 117.0.5938.132). My CPU is i9 13900K so it's definitely not a hardware issue. @wxfred's proposal solves it, but then I lose TypeScript types.

SvetlozarValchev avatar Oct 06 '23 20:10 SvetlozarValchev

@SvetlozarValchev yeah sorry about that - I'm hoping to do a big rewrite of this when I get to it with my current project that should fix these issues. A lot of the code in the repo (e.g. gifjs, CCapture, all the ffmpeg stuff) was not written by me and it does not seem to be playing well in compiled js environments.

amandaghassaei avatar Oct 11 '23 01:10 amandaghassaei