[英]ERROR TypeError: fs.createWriteStream is not a function Angular 6 GIF export
I have created the following function to create GIF and I want to export it to a .gif image. 我创建了以下函数来创建GIF,并将其导出到.gif图像。
import * as gifEncoder from 'gif-encoder';
animate() {
var canvas: HTMLCanvasElement = this.canvas.nativeElement;
var context = canvas.getContext('2d');
var gif = gifEncoder;
gif = new gifEncoder(10, 10);
if (this.numberOfFrames < this.points.length - 1) {
requestAnimationFrame(() => this.animate());
}
else {
console.log("Program End Reached...");
gif.finish();
this.gif = gif;
}
context.beginPath();
var fromX = this.points[this.numberOfFrames - 1].x;
var fromY = this.points[this.numberOfFrames - 1].y;
var toX = this.points[this.numberOfFrames].x;
var toY = this.points[this.numberOfFrames].y;
context.lineWidth = 5;
context.strokeStyle = "red";
context.moveTo(fromX, fromY);
context.lineTo(toX, toY);
context.stroke();
gif.writeHeader();
gif.addFrame(context);
this.numberOfFrames++;
}
Then I have a button to export this canvas content to a GIF file. 然后,我有一个按钮可以将该画布内容导出到GIF文件。
downloadAsGIF() {
let fs = require('fs');
var file = fs.createWriteStream('img.gif');
this.gif.pipe(file);
}
Error: 错误:
ERROR TypeError: fs.createWriteStream is not a function
Where am I going wrong? 我要去哪里错了? Any help would be apppreciated. 任何帮助将不胜感激。 I am using this tutorial 我正在使用本教程
Actually that will run on server side. 实际上,它将在服务器端运行。 Browser don't have fs
. 浏览器没有fs
。 So you gotta use some other library which is supported by browser. 因此,您必须使用浏览器支持的其他一些库。
That said, here's a client side JS answer. 也就是说,这是客户端JS答案。
Using 使用
Using BrowserFS.configure(), you can easily configure BrowserFS to use a variety of file system types. 使用BrowserFS.configure(),可以轻松配置BrowserFS以使用各种文件系统类型。
Here's a simple usage example using the LocalStorage-backed file system: 这是一个使用LocalStorage支持的文件系统的简单用法示例:
<script type="text/javascript" src="browserfs.min.js"></script>
<script type="text/javascript">
// Installs globals onto window:
// * Buffer
// * require (monkey-patches if already defined)
// * process
// You can pass in an arbitrary object if you do not wish to pollute
// the global namespace.
BrowserFS.install(window);
// Configures BrowserFS to use the LocalStorage file system.
BrowserFS.configure({
fs: "LocalStorage"
}, function(e) {
if (e) {
// An error happened!
throw e;
}
// Otherwise, BrowserFS is ready-to-use!
});
</script>
Can easily use like this: 可以像这样轻松使用:
var fs = require('fs');
fs.writeFile('/test.txt', 'Cool, I can do this in the browser!', function(err) {
fs.readFile('/test.txt', function(err, contents) {
console.log(contents.toString());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.