简体   繁体   English

错误TypeError:fs.​​createWriteStream不是函数Angular 6 GIF导出

[英]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.

相关问题 未捕获的TypeError:fs.​​createWriteStream不是函数 - Uncaught TypeError: fs.createWriteStream is not a function 类型错误:fs.createWriteStream 不是 Node 中的 function - TypeError: fs.createWriteStream is not a function in Node ExcelJS writeFile 不起作用:Uncaught TypeError: fs.createWriteStream is not a function - ExcelJS writeFile not working: Uncaught TypeError: fs.createWriteStream is not a function 为什么 fs.createWriteStream 抛出“错误:ENOENT:没有这样的文件或目录”错误? - Why is fs.createWriteStream throwing a "Error: ENOENT: no such file or directory" error? Node.js fs.createWriteStream 未定义 function - Node.js fs.createWriteStream undefined function 错误事件上的fs.createWriteStream无法捕获错误 - fs.createWriteStream on error event does not catch errors 如何确保fs.createWriteStream在函数继续执行之前完成; 仅保存一部分图像 - How to make sure fs.createWriteStream finishes before function continues; only fraction of image being saved 使用fs.createWriteStream在Node.js中写入大文件 - Writing large file in Nodejs using fs.createWriteStream 是否可以使用 fs.createWriteStream 在文件中间写入文本? (或一般在nodejs中) - Is it possible to write text in the middle of a file with fs.createWriteStream ? (or in nodejs in general) 自动关闭“fs.createWriteStream()”以避免潜在的 memory 泄漏 - Auto close of “fs.createWriteStream()” to avoid potential memory leak
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM