Created
May 21, 2020 15:17
-
-
Save kulikala/039d7c94f46ee860f1f9e2714104cb90 to your computer and use it in GitHub Desktop.
Serious canvas memory leak in Safari (both iOS / macOS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Safari Memory Leak</title> | |
</head> | |
<body> | |
<div id="wrapper"> | |
<div id="controls"> | |
<button id="play">Play</button> | |
<button id="pause">Pause</button> | |
<label for="toggle"> | |
<input type="checkbox" id="toggle"> | |
Invoke memory leak | |
</label> | |
</div> | |
<div id="container"> | |
<canvas id="canvas"></canvas> | |
</div> | |
</div> | |
<script> | |
const url = 'https://www.pexels.com/video/2098989/download/?search_query=&tracking_id=aace72bic2s&w=3840&h=2160' | |
const video = document.createElement('video') | |
video.setAttribute('playsinline', '') | |
video.setAttribute('preload', 'auto') | |
video.src = url | |
const canvas = document.getElementById('canvas') | |
const toggle = document.getElementById('toggle') | |
let requestId = 0 | |
document.getElementById('play').addEventListener('click', async () => { | |
await video.play() | |
canvas.width = video.videoWidth / 8 | |
canvas.height = video.videoHeight / 8 | |
drawImage() | |
}) | |
document.getElementById('pause').addEventListener('click', () => { | |
video.pause() | |
cancelAnimationFrame(requestId) | |
}) | |
function drawImage () { | |
const ctx = canvas.getContext('2d') | |
if (toggle.checked) { | |
ctx.drawImage( | |
video, | |
video.videoWidth / 4, video.videoHeight / 4, | |
video.videoWidth / 2, video.videoHeight / 2, | |
0, 0, | |
canvas.width, canvas.height | |
) | |
} else { | |
ctx.drawImage( | |
video, | |
0, 0, | |
video.videoWidth, video.videoHeight, | |
0, 0, | |
canvas.width, canvas.height | |
) | |
} | |
requestId = requestAnimationFrame(drawImage) | |
} | |
</script> | |
</body> | |
</html> |
Set the canvas's width and height properties to 0 before it is garbage collected. Likewise, set the video's src property to //:0
or any other invalid URL.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello ~ have you found any solution to skip this problem?