Browse Source

Fix w/ pool of canvases

Noah 8 years ago
parent
commit
2a13985fa2
1 changed files with 25 additions and 8 deletions
  1. 25 8
      audiogram/draw-frames.js

+ 25 - 8
audiogram/draw-frames.js View File

@@ -6,8 +6,11 @@ var fs = require("fs"),
6 6
 function drawFrames(renderer, options, cb) {
7 7
 
8 8
   var frameQueue = queue(10),
9
-      canvas = new Canvas(options.width, options.height),
10
-      context = canvas.getContext("2d");
9
+      canvases = [];
10
+
11
+  for (var i = 0; i < 10; i++) {
12
+    canvases.push(new Canvas(options.width, options.height));
13
+  }
11 14
 
12 15
   for (var i = 0; i < options.numFrames; i++) {
13 16
     frameQueue.defer(drawFrame, i);
@@ -17,22 +20,36 @@ function drawFrames(renderer, options, cb) {
17 20
 
18 21
   function drawFrame(frameNumber, frameCallback) {
19 22
 
23
+    var canvas = canvases.pop(),
24
+        context = canvas.getContext("2d");
25
+
20 26
     renderer.drawFrame(context, {
21 27
       caption: options.caption,
22 28
       waveform: options.waveform[frameNumber],
23 29
       frame: frameNumber
24 30
     });
25 31
 
26
-    fs.writeFile(path.join(options.frameDir, zeropad(frameNumber + 1, 6) + ".png"), canvas.toBuffer(), function(err) {
32
+    canvas.toBuffer(function(err, buf){
33
+
27 34
       if (err) {
28
-        return frameCallback(err);
35
+        return cb(err);
29 36
       }
30 37
 
31
-      if (options.tick) {
32
-        options.tick();
33
-      }
38
+      fs.writeFile(path.join(options.frameDir, zeropad(frameNumber + 1, 6) + ".png"), buf, function(writeErr) {
39
+
40
+        if (writeErr) {
41
+          return frameCallback(writeErr);
42
+        }
43
+
44
+        if (options.tick) {
45
+          options.tick();
46
+        }
47
+
48
+        canvases.push(canvas);
49
+
50
+        return frameCallback(null);
34 51
 
35
-      return frameCallback(null);
52
+      });
36 53
 
37 54
     });
38 55