简体   繁体   English

Javascript Canvas 迭代自动保存图片到文件夹

[英]Javascript Canvas iteratively save image to folder automatically

I am running some generative art functions to create an image in HTML/JS canvas.我正在运行一些生成艺术功能以在 HTML/JS canvas 中创建图像。 I have a variable in a loop that I would like to increment in the function and regenerate the image many times thus creating a succession of gradually changing images I would like to "stitch" together as a video showing a systematic evolution.我在循环中有一个变量,我想在 function 中递增并多次重新生成图像,从而创建一系列逐渐变化的图像,我想将其“缝合”在一起作为显示系统演变的视频。 I do not want to use a download link or right click on the images to save them.我不想使用下载链接或右键单击图像来保存它们。 I would like to specify a folder location on my PC and have the infdividual images automatically save to the location.我想在我的电脑上指定一个文件夹位置,并让个人图像自动保存到该位置。 Is this possible?这可能吗?

For the first part of your question, you can capture animations created in a canvas element using the MediaRecorder interface of the browser's MediaStream Recording API.对于问题的第一部分,您可以使用浏览器的 MediaStream Recording API 的 MediaRecorder 接口捕获在 canvas 元素中创建的动画。 MDN have some details about how to set this up here: https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API MDN 有一些关于如何在此处进行设置的详细信息: https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API

The second part of your question is more difficult - browsers are sandboxed from the user's device for security reasons.您问题的第二部分更难 - 出于安全原因,浏览器从用户设备被沙盒化。 There is the new File System Access API - as described in this WebDev article - but I've never used it so cannot say how useful it would be for your use case.有新的文件系统访问 API -如本 WebDev 文章中所述- 但我从未使用过它,所以不能说它对您的用例有多大用处。

Alternatively, you could send the video, or image snapshots, to a remote file storage facility and download them from there.或者,您可以将视频或图像快照发送到远程文件存储设施并从那里下载。

If this is a one-off for yourself and not a public website, you can use node-canvas , and have full filesystem access.如果这是一次性的,而不是公共网站,您可以使用node-canvas并拥有完整的文件系统访问权限。 You can then use ffmpeg to stitch frames together:然后,您可以使用 ffmpeg 将帧拼接在一起:

ffmpeg -framerate 30 -i /path/to/folder/fileName.%02d.png

Use %02d if your images are labeled 10-99, %03d for 100-999, etc.如果您的图像标记为 10-99,则使用%02d ,使用 % %03d标记 100-999 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM