Kevin Lubick | 53eabf6 | 2018-12-10 12:41:26 -0500 | [diff] [blame] | 1 | function CanvasPattern(image, repetition) { |
| 2 | this._shader = null; |
| 3 | // image should be an SkImage returned from HTMLCanvas.decodeImage() |
| 4 | this._image = image; |
| 5 | this._transform = CanvasKit.SkMatrix.identity(); |
| 6 | |
| 7 | if (repetition === '') { |
| 8 | repetition = 'repeat'; |
| 9 | } |
| 10 | switch(repetition) { |
| 11 | case 'repeat-x': |
| 12 | this._tileX = CanvasKit.TileMode.Repeat; |
| 13 | // Skia's 'clamp' mode repeats the last row/column |
| 14 | // which looks very very strange. |
| 15 | // Decal mode does just transparent copying, which |
| 16 | // is exactly what the spec wants. |
| 17 | this._tileY = CanvasKit.TileMode.Decal; |
| 18 | break; |
| 19 | case 'repeat-y': |
| 20 | this._tileX = CanvasKit.TileMode.Decal; |
| 21 | this._tileY = CanvasKit.TileMode.Repeat; |
| 22 | break; |
| 23 | case 'repeat': |
| 24 | this._tileX = CanvasKit.TileMode.Repeat; |
| 25 | this._tileY = CanvasKit.TileMode.Repeat; |
| 26 | break; |
| 27 | case 'no-repeat': |
| 28 | this._tileX = CanvasKit.TileMode.Decal; |
| 29 | this._tileY = CanvasKit.TileMode.Decal; |
| 30 | break; |
| 31 | default: |
| 32 | throw 'invalid repetition mode ' + repetition; |
| 33 | } |
| 34 | |
| 35 | // Takes a DOMMatrix like object. e.g. the identity would be: |
| 36 | // {a:1, b: 0, c: 0, d: 1, e: 0, f: 0} |
| 37 | // @param {DOMMatrix} m |
| 38 | this.setTransform = function(m) { |
| 39 | var t = [m.a, m.c, m.e, |
| 40 | m.b, m.d, m.f, |
| 41 | 0, 0, 1]; |
| 42 | if (allAreFinite(t)) { |
| 43 | this._transform = t; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | this._copy = function() { |
| 48 | var cp = new CanvasPattern() |
| 49 | cp._tileX = this._tileX; |
| 50 | cp._tileY = this._tileY; |
| 51 | return cp; |
| 52 | } |
| 53 | |
| 54 | this._dispose = function() { |
| 55 | if (this._shader) { |
| 56 | this._shader.delete(); |
| 57 | this._shader = null; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | this._getShader = function(currentTransform) { |
| 62 | // Ignore currentTransform since it will be applied later |
| 63 | this._dispose(); |
| 64 | this._shader = CanvasKit.MakeImageShader(this._image, this._tileX, this._tileY, |
| 65 | false, this._transform); |
| 66 | return this._shader; |
| 67 | } |
| 68 | |
| 69 | } |