blob: ab6f990a85f56e1bfebda934def8488c680e396b [file] [log] [blame]
Kevin Lubick53eabf62018-12-10 12:41:26 -05001function CanvasPattern(image, repetition) {
2 this._shader = null;
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04003 // image should be an Image returned from HTMLCanvas.decodeImage()
Kevin Lubick53eabf62018-12-10 12:41:26 -05004 this._image = image;
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04005 this._transform = CanvasKit.Matrix.identity();
Kevin Lubick53eabf62018-12-10 12:41:26 -05006
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();
Kevin Lubicka064c282019-04-04 09:28:53 -040064 this._shader = this._image.makeShader(this._tileX, this._tileY, this._transform);
Kevin Lubick53eabf62018-12-10 12:41:26 -050065 return this._shader;
66 }
67
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040068}