• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Adds compile-time JS functions to augment the SkottieKit interface.
2// Specifically, anything that should only be on the CPU version of SkottieKit.
3(function(SkottieKit){
4SkottieKit._extraInitializations = SkottieKit._extraInitializations || [];
5SkottieKit._extraInitializations.push(function() {
6  // Takes in an html id or a canvas element
7  SkottieKit.MakeSWCanvasSurface = function(idOrElement) {
8      var canvas = idOrElement;
9      if (canvas.tagName !== 'CANVAS') {
10        canvas = document.getElementById(idOrElement);
11        if (!canvas) {
12          throw 'Canvas with id ' + idOrElement + ' was not found';
13        }
14      }
15    // Maybe better to use clientWidth/height.  See:
16    // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
17    var surface = SkottieKit.MakeInMemorySurface(canvas.width, canvas.height);
18    if (surface) {
19      surface._canvas = canvas;
20    }
21    return surface;
22  };
23
24  // Don't over-write the MakeCanvasSurface set by gpu.js if it exists.
25  if (!SkottieKit.MakeCanvasSurface) {
26    SkottieKit.MakeCanvasSurface = SkottieKit.MakeSWCanvasSurface;
27  }
28
29  SkottieKit.MakeInMemorySurface = function(width, height) {
30    /* @dict */
31    var imageInfo = {
32      'width':  width,
33      'height': height,
34      'colorType': SkottieKit.ColorType.RGBA_8888,
35      // Since we are sending these pixels directly into the HTML canvas,
36      // (and those pixels are un-premultiplied, i.e. straight r,g,b,a)
37      'alphaType': SkottieKit.AlphaType.Unpremul,
38    }
39    var pixelLen = width * height * 4; // it's 8888, so 4 bytes per pixel
40    // Allocate the buffer of pixels to be drawn into.
41    var pixelPtr = SkottieKit._malloc(pixelLen);
42
43    var surface = this._getRasterDirectSurface(imageInfo, pixelPtr, width*4);
44    if (surface) {
45      surface._canvas = null;
46      surface._width = width;
47      surface._height = height;
48      surface._pixelLen = pixelLen;
49
50      surface._pixelPtr = pixelPtr;
51      // rasterDirectSurface does not initialize the pixels, so we clear them
52      // to transparent black.
53      surface.getCanvas().clear(SkottieKit.TRANSPARENT);
54    }
55    return surface;
56  };
57
58  SkottieKit.SkSurface.prototype.flush = function() {
59    this._flush();
60    // Do we have an HTML canvas to write the pixels to?
61    // We will not if this a GPU build or a raster surface, for example.
62    if (this._canvas) {
63      var pixels = new Uint8ClampedArray(SkottieKit.HEAPU8.buffer, this._pixelPtr, this._pixelLen);
64      var imageData = new ImageData(pixels, this._width, this._height);
65
66      this._canvas.getContext('2d').putImageData(imageData, 0, 0);
67    }
68  };
69
70  // Call dispose() instead of delete to clean up the underlying memory
71  SkottieKit.SkSurface.prototype.dispose = function() {
72    if (this._pixelPtr) {
73      SkottieKit._free(this._pixelPtr);
74    }
75    this.delete();
76  }
77
78  SkottieKit.currentContext = SkottieKit.currentContext || function() {
79    // no op if this is a cpu-only build.
80  };
81
82  SkottieKit.setCurrentContext = SkottieKit.setCurrentContext || function() {
83     // no op if this is a cpu-only build.
84  };
85});
86}(Module)); // When this file is loaded in, the high level object is "Module";
87
88