-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bitmap.js
1054 lines (959 loc) · 29 KB
/
Bitmap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
/**
* The basic object that represents an image.
*
* @class Bitmap
* @constructor
* @param {Number} width The width of the bitmap
* @param {Number} height The height of the bitmap
*/
function Bitmap() {
this.initialize.apply(this, arguments);
}
//for iOS. img consumes memory. so reuse it.
Bitmap._reuseImages = [];
/**
* Bitmap states(Bitmap._loadingState):
*
* none:
* Empty Bitmap
*
* pending:
* Url requested, but pending to load until startRequest called
*
* purged:
* Url request completed and purged.
*
* requesting:
* Requesting supplied URI now.
*
* requestCompleted:
* Request completed
*
* decrypting:
* requesting encrypted data from supplied URI or decrypting it.
*
* decryptCompleted:
* Decrypt completed
*
* loaded:
* loaded. isReady() === true, so It's usable.
*
* error:
* error occurred
*
*/
Bitmap.prototype._createCanvas = function(width, height){
this.__canvas = this.__canvas || document.createElement('canvas');
this.__context = this.__canvas.getContext('2d');
this.__canvas.width = Math.max(width || 0, 1);
this.__canvas.height = Math.max(height || 0, 1);
if(this._image){
var w = Math.max(this._image.width || 0, 1);
var h = Math.max(this._image.height || 0, 1);
this.__canvas.width = w;
this.__canvas.height = h;
this._createBaseTexture(this._canvas);
this.__context.drawImage(this._image, 0, 0);
}
this._setDirty();
};
Bitmap.prototype._createBaseTexture = function(source){
this.__baseTexture = new PIXI.BaseTexture(source);
this.__baseTexture.mipmap = false;
this.__baseTexture.width = source.width;
this.__baseTexture.height = source.height;
if (this._smooth) {
this._baseTexture.scaleMode = PIXI.SCALE_MODES.LINEAR;
} else {
this._baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
}
};
Bitmap.prototype._clearImgInstance = function(){
this._image.src = "";
this._image.onload = null;
this._image.onerror = null;
this._errorListener = null;
this._loadListener = null;
Bitmap._reuseImages.push(this._image);
this._image = null;
};
//
//We don't want to waste memory, so creating canvas is deferred.
//
Object.defineProperties(Bitmap.prototype, {
_canvas: {
get: function(){
if(!this.__canvas)this._createCanvas();
return this.__canvas;
}
},
_context: {
get: function(){
if(!this.__context)this._createCanvas();
return this.__context;
}
},
_baseTexture: {
get: function(){
if(!this.__baseTexture) this._createBaseTexture(this._image || this.__canvas);
return this.__baseTexture;
}
}
});
Bitmap.prototype._renewCanvas = function(){
var newImage = this._image;
if(newImage && this.__canvas && (this.__canvas.width < newImage.width || this.__canvas.height < newImage.height)){
this._createCanvas();
}
};
Bitmap.prototype.initialize = function(width, height) {
if(!this._defer){
this._createCanvas(width, height);
}
this._image = null;
this._url = '';
this._paintOpacity = 255;
this._smooth = false;
this._loadListeners = [];
this._loadingState = 'none';
this._decodeAfterRequest = false;
/**
* Cache entry, for images. In all cases _url is the same as cacheEntry.key
* @type CacheEntry
*/
this.cacheEntry = null;
/**
* The face name of the font.
*
* @property fontFace
* @type String
*/
this.fontFace = 'GameFont';
/**
* The size of the font in pixels.
*
* @property fontSize
* @type Number
*/
this.fontSize = 28;
/**
* Whether the font is italic.
*
* @property fontItalic
* @type Boolean
*/
this.fontItalic = false;
/**
* The color of the text in CSS format.
*
* @property textColor
* @type String
*/
this.textColor = '#ffffff';
/**
* The color of the outline of the text in CSS format.
*
* @property outlineColor
* @type String
*/
this.outlineColor = 'rgba(0, 0, 0, 0.5)';
/**
* The width of the outline of the text.
*
* @property outlineWidth
* @type Number
*/
this.outlineWidth = 4;
};
/**
* Loads a image file and returns a new bitmap object.
*
* @static
* @method load
* @param {String} url The image url of the texture
* @return Bitmap
*/
Bitmap.load = function(url) {
var bitmap = Object.create(Bitmap.prototype);
bitmap._defer = true;
bitmap.initialize();
bitmap._decodeAfterRequest = true;
bitmap._requestImage(url);
return bitmap;
};
/**
* Takes a snapshot of the game screen and returns a new bitmap object.
*
* @static
* @method snap
* @param {Stage} stage The stage object
* @return Bitmap
*/
Bitmap.snap = function(stage) {
var width = Graphics.width;
var height = Graphics.height;
var bitmap = new Bitmap(width, height);
var context = bitmap._context;
var renderTexture = PIXI.RenderTexture.create(width, height);
if (stage) {
Graphics._renderer.render(stage, renderTexture);
stage.worldTransform.identity();
var canvas = null;
if (Graphics.isWebGL()) {
canvas = Graphics._renderer.extract.canvas(renderTexture);
} else {
canvas = renderTexture.baseTexture._canvasRenderTarget.canvas;
}
context.drawImage(canvas, 0, 0);
} else {
}
renderTexture.destroy({ destroyBase: true });
bitmap._setDirty();
return bitmap;
};
/**
* Checks whether the bitmap is ready to render.
*
* @method isReady
* @return {Boolean} True if the bitmap is ready to render
*/
Bitmap.prototype.isReady = function() {
return this._loadingState === 'loaded' || this._loadingState === 'none';
};
/**
* Checks whether a loading error has occurred.
*
* @method isError
* @return {Boolean} True if a loading error has occurred
*/
Bitmap.prototype.isError = function() {
return this._loadingState === 'error';
};
/**
* touch the resource
* @method touch
*/
Bitmap.prototype.touch = function() {
if (this.cacheEntry) {
this.cacheEntry.touch();
}
};
/**
* [read-only] The url of the image file.
*
* @property url
* @type String
*/
Object.defineProperty(Bitmap.prototype, 'url', {
get: function() {
return this._url;
},
configurable: true
});
/**
* [read-only] The base texture that holds the image.
*
* @property baseTexture
* @type PIXI.BaseTexture
*/
Object.defineProperty(Bitmap.prototype, 'baseTexture', {
get: function() {
return this._baseTexture;
},
configurable: true
});
/**
* [read-only] The bitmap canvas.
*
* @property canvas
* @type HTMLCanvasElement
*/
Object.defineProperty(Bitmap.prototype, 'canvas', {
get: function() {
return this._canvas;
},
configurable: true
});
/**
* [read-only] The 2d context of the bitmap canvas.
*
* @property context
* @type CanvasRenderingContext2D
*/
Object.defineProperty(Bitmap.prototype, 'context', {
get: function() {
return this._context;
},
configurable: true
});
/**
* [read-only] The width of the bitmap.
*
* @property width
* @type Number
*/
Object.defineProperty(Bitmap.prototype, 'width', {
get: function() {
if(this.isReady()){
return this._image? this._image.width: this._canvas.width;
}
return 0;
},
configurable: true
});
/**
* [read-only] The height of the bitmap.
*
* @property height
* @type Number
*/
Object.defineProperty(Bitmap.prototype, 'height', {
get: function() {
if(this.isReady()){
return this._image? this._image.height: this._canvas.height;
}
return 0;
},
configurable: true
});
/**
* [read-only] The rectangle of the bitmap.
*
* @property rect
* @type Rectangle
*/
Object.defineProperty(Bitmap.prototype, 'rect', {
get: function() {
return new Rectangle(0, 0, this.width, this.height);
},
configurable: true
});
/**
* Whether the smooth scaling is applied.
*
* @property smooth
* @type Boolean
*/
Object.defineProperty(Bitmap.prototype, 'smooth', {
get: function() {
return this._smooth;
},
set: function(value) {
if (this._smooth !== value) {
this._smooth = value;
if(this.__baseTexture){
if (this._smooth) {
this._baseTexture.scaleMode = PIXI.SCALE_MODES.LINEAR;
} else {
this._baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
}
}
}
},
configurable: true
});
/**
* The opacity of the drawing object in the range (0, 255).
*
* @property paintOpacity
* @type Number
*/
Object.defineProperty(Bitmap.prototype, 'paintOpacity', {
get: function() {
return this._paintOpacity;
},
set: function(value) {
if (this._paintOpacity !== value) {
this._paintOpacity = value;
this._context.globalAlpha = this._paintOpacity / 255;
}
},
configurable: true
});
/**
* Resizes the bitmap.
*
* @method resize
* @param {Number} width The new width of the bitmap
* @param {Number} height The new height of the bitmap
*/
Bitmap.prototype.resize = function(width, height) {
width = Math.max(width || 0, 1);
height = Math.max(height || 0, 1);
this._canvas.width = width;
this._canvas.height = height;
this._baseTexture.width = width;
this._baseTexture.height = height;
};
/**
* Performs a block transfer.
*
* @method blt
* @param {Bitmap} source The bitmap to draw
* @param {Number} sx The x coordinate in the source
* @param {Number} sy The y coordinate in the source
* @param {Number} sw The width of the source image
* @param {Number} sh The height of the source image
* @param {Number} dx The x coordinate in the destination
* @param {Number} dy The y coordinate in the destination
* @param {Number} [dw=sw] The width to draw the image in the destination
* @param {Number} [dh=sh] The height to draw the image in the destination
*/
Bitmap.prototype.blt = function(source, sx, sy, sw, sh, dx, dy, dw, dh) {
dw = dw || sw;
dh = dh || sh;
if (sx >= 0 && sy >= 0 && sw > 0 && sh > 0 && dw > 0 && dh > 0 &&
sx + sw <= source.width && sy + sh <= source.height) {
this._context.globalCompositeOperation = 'source-over';
this._context.drawImage(source._canvas, sx, sy, sw, sh, dx, dy, dw, dh);
this._setDirty();
}
};
/**
* Performs a block transfer, using assumption that original image was not modified (no hue)
*
* @method blt
* @param {Bitmap} source The bitmap to draw
* @param {Number} sx The x coordinate in the source
* @param {Number} sy The y coordinate in the source
* @param {Number} sw The width of the source image
* @param {Number} sh The height of the source image
* @param {Number} dx The x coordinate in the destination
* @param {Number} dy The y coordinate in the destination
* @param {Number} [dw=sw] The width to draw the image in the destination
* @param {Number} [dh=sh] The height to draw the image in the destination
*/
Bitmap.prototype.bltImage = function(source, sx, sy, sw, sh, dx, dy, dw, dh) {
dw = dw || sw;
dh = dh || sh;
if (sx >= 0 && sy >= 0 && sw > 0 && sh > 0 && dw > 0 && dh > 0 &&
sx + sw <= source.width && sy + sh <= source.height) {
this._context.globalCompositeOperation = 'source-over';
this._context.drawImage(source._image, sx, sy, sw, sh, dx, dy, dw, dh);
this._setDirty();
}
};
/**
* Returns pixel color at the specified point.
*
* @method getPixel
* @param {Number} x The x coordinate of the pixel in the bitmap
* @param {Number} y The y coordinate of the pixel in the bitmap
* @return {String} The pixel color (hex format)
*/
Bitmap.prototype.getPixel = function(x, y) {
var data = this._context.getImageData(x, y, 1, 1).data;
var result = '#';
for (var i = 0; i < 3; i++) {
result += data[i].toString(16).padZero(2);
}
return result;
};
/**
* Returns alpha pixel value at the specified point.
*
* @method getAlphaPixel
* @param {Number} x The x coordinate of the pixel in the bitmap
* @param {Number} y The y coordinate of the pixel in the bitmap
* @return {String} The alpha value
*/
Bitmap.prototype.getAlphaPixel = function(x, y) {
var data = this._context.getImageData(x, y, 1, 1).data;
return data[3];
};
/**
* Clears the specified rectangle.
*
* @method clearRect
* @param {Number} x The x coordinate for the upper-left corner
* @param {Number} y The y coordinate for the upper-left corner
* @param {Number} width The width of the rectangle to clear
* @param {Number} height The height of the rectangle to clear
*/
Bitmap.prototype.clearRect = function(x, y, width, height) {
this._context.clearRect(x, y, width, height);
this._setDirty();
};
/**
* Clears the entire bitmap.
*
* @method clear
*/
Bitmap.prototype.clear = function() {
this.clearRect(0, 0, this.width, this.height);
};
/**
* Fills the specified rectangle.
*
* @method fillRect
* @param {Number} x The x coordinate for the upper-left corner
* @param {Number} y The y coordinate for the upper-left corner
* @param {Number} width The width of the rectangle to fill
* @param {Number} height The height of the rectangle to fill
* @param {String} color The color of the rectangle in CSS format
*/
Bitmap.prototype.fillRect = function(x, y, width, height, color) {
var context = this._context;
context.save();
context.fillStyle = color;
context.fillRect(x, y, width, height);
context.restore();
this._setDirty();
};
/**
* Fills the entire bitmap.
*
* @method fillAll
* @param {String} color The color of the rectangle in CSS format
*/
Bitmap.prototype.fillAll = function(color) {
this.fillRect(0, 0, this.width, this.height, color);
};
/**
* Draws the rectangle with a gradation.
*
* @method gradientFillRect
* @param {Number} x The x coordinate for the upper-left corner
* @param {Number} y The y coordinate for the upper-left corner
* @param {Number} width The width of the rectangle to fill
* @param {Number} height The height of the rectangle to fill
* @param {String} color1 The gradient starting color
* @param {String} color2 The gradient ending color
* @param {Boolean} vertical Wether the gradient should be draw as vertical or not
*/
Bitmap.prototype.gradientFillRect = function(x, y, width, height, color1,
color2, vertical) {
var context = this._context;
var grad;
if (vertical) {
grad = context.createLinearGradient(x, y, x, y + height);
} else {
grad = context.createLinearGradient(x, y, x + width, y);
}
grad.addColorStop(0, color1);
grad.addColorStop(1, color2);
context.save();
context.fillStyle = grad;
context.fillRect(x, y, width, height);
context.restore();
this._setDirty();
};
/**
* Draw a bitmap in the shape of a circle
*
* @method drawCircle
* @param {Number} x The x coordinate based on the circle center
* @param {Number} y The y coordinate based on the circle center
* @param {Number} radius The radius of the circle
* @param {String} color The color of the circle in CSS format
*/
Bitmap.prototype.drawCircle = function(x, y, radius, color) {
var context = this._context;
context.save();
context.fillStyle = color;
context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2, false);
context.fill();
context.restore();
this._setDirty();
};
/**
* Draws the outline text to the bitmap.
*
* @method drawText
* @param {String} text The text that will be drawn
* @param {Number} x The x coordinate for the left of the text
* @param {Number} y The y coordinate for the top of the text
* @param {Number} maxWidth The maximum allowed width of the text
* @param {Number} lineHeight The height of the text line
* @param {String} align The alignment of the text
*/
Bitmap.prototype.drawText = function(text, x, y, maxWidth, lineHeight, align) {
// Note: Firefox has a bug with textBaseline: Bug 737852
// So we use 'alphabetic' here.
if (text !== undefined) {
if (this.fontSize < Bitmap.minFontSize) {
this.drawSmallText(text, x, y, maxWidth, lineHeight, align);
return;
}
var tx = x;
var ty = y + lineHeight - Math.round((lineHeight - this.fontSize * 0.7) / 2);
var context = this._context;
var alpha = context.globalAlpha;
maxWidth = maxWidth || 0xffffffff;
if (align === 'center') {
tx += maxWidth / 2;
}
if (align === 'right') {
tx += maxWidth;
}
context.save();
context.font = this._makeFontNameText();
context.textAlign = align;
context.textBaseline = 'alphabetic';
context.globalAlpha = 1;
this._drawTextOutline(text, tx, ty, maxWidth);
context.globalAlpha = alpha;
this._drawTextBody(text, tx, ty, maxWidth);
context.restore();
this._setDirty();
}
};
/**
* Draws the small text big once and resize it because modern broswers are poor at drawing small text.
*
* @method drawSmallText
* @param {String} text The text that will be drawn
* @param {Number} x The x coordinate for the left of the text
* @param {Number} y The y coordinate for the top of the text
* @param {Number} maxWidth The maximum allowed width of the text
* @param {Number} lineHeight The height of the text line
* @param {String} align The alignment of the text
*/
Bitmap.prototype.drawSmallText = function(text, x, y, maxWidth, lineHeight, align) {
var minFontSize = Bitmap.minFontSize;
var bitmap = Bitmap.drawSmallTextBitmap;
bitmap.fontFace = this.fontFace;
bitmap.fontSize = minFontSize;
bitmap.fontItalic = this.fontItalic;
bitmap.textColor = this.textColor;
bitmap.outlineColor = this.outlineColor;
bitmap.outlineWidth = this.outlineWidth * minFontSize / this.fontSize;
maxWidth = maxWidth || 816;
var height = this.fontSize * 1.5;
var scaledMaxWidth = maxWidth * minFontSize / this.fontSize;
var scaledMaxWidthWithOutline = scaledMaxWidth + bitmap.outlineWidth * 2;
var scaledHeight = height * minFontSize / this.fontSize;
var scaledHeightWithOutline = scaledHeight + bitmap.outlineWidth * 2;
var bitmapWidth = bitmap.width;
var bitmapHeight = bitmap.height;
while (scaledMaxWidthWithOutline > bitmapWidth) bitmapWidth *= 2;
while (scaledHeightWithOutline > bitmapHeight) bitmapHeight *= 2;
if (bitmap.width !== bitmapWidth || bitmap.height !== bitmapHeight) bitmap.resize(bitmapWidth, bitmapHeight);
bitmap.drawText(text, bitmap.outlineWidth, bitmap.outlineWidth, scaledMaxWidth, minFontSize, align);
this.blt(bitmap, 0, 0, scaledMaxWidthWithOutline, scaledHeightWithOutline,
x - this.outlineWidth, y - this.outlineWidth + (lineHeight - this.fontSize) / 2, maxWidth + this.outlineWidth * 2, height + this.outlineWidth * 2);
bitmap.clear();
};
/**
* Returns the width of the specified text.
*
* @method measureTextWidth
* @param {String} text The text to be measured
* @return {Number} The width of the text in pixels
*/
Bitmap.prototype.measureTextWidth = function(text) {
var context = this._context;
context.save();
context.font = this._makeFontNameText();
var width = context.measureText(text).width;
context.restore();
return width;
};
/**
* Changes the color tone of the entire bitmap.
*
* @method adjustTone
* @param {Number} r The red strength in the range (-255, 255)
* @param {Number} g The green strength in the range (-255, 255)
* @param {Number} b The blue strength in the range (-255, 255)
*/
Bitmap.prototype.adjustTone = function(r, g, b) {
if ((r || g || b) && this.width > 0 && this.height > 0) {
var context = this._context;
var imageData = context.getImageData(0, 0, this.width, this.height);
var pixels = imageData.data;
for (var i = 0; i < pixels.length; i += 4) {
pixels[i + 0] += r;
pixels[i + 1] += g;
pixels[i + 2] += b;
}
context.putImageData(imageData, 0, 0);
this._setDirty();
}
};
/**
* Rotates the hue of the entire bitmap.
*
* @method rotateHue
* @param {Number} offset The hue offset in 360 degrees
*/
Bitmap.prototype.rotateHue = function(offset) {
function rgbToHsl(r, g, b) {
var cmin = Math.min(r, g, b);
var cmax = Math.max(r, g, b);
var h = 0;
var s = 0;
var l = (cmin + cmax) / 2;
var delta = cmax - cmin;
if (delta > 0) {
if (r === cmax) {
h = 60 * (((g - b) / delta + 6) % 6);
} else if (g === cmax) {
h = 60 * ((b - r) / delta + 2);
} else {
h = 60 * ((r - g) / delta + 4);
}
s = delta / (255 - Math.abs(2 * l - 255));
}
return [h, s, l];
}
function hslToRgb(h, s, l) {
var c = (255 - Math.abs(2 * l - 255)) * s;
var x = c * (1 - Math.abs((h / 60) % 2 - 1));
var m = l - c / 2;
var cm = c + m;
var xm = x + m;
if (h < 60) {
return [cm, xm, m];
} else if (h < 120) {
return [xm, cm, m];
} else if (h < 180) {
return [m, cm, xm];
} else if (h < 240) {
return [m, xm, cm];
} else if (h < 300) {
return [xm, m, cm];
} else {
return [cm, m, xm];
}
}
if (offset && this.width > 0 && this.height > 0) {
offset = ((offset % 360) + 360) % 360;
var context = this._context;
var imageData = context.getImageData(0, 0, this.width, this.height);
var pixels = imageData.data;
for (var i = 0; i < pixels.length; i += 4) {
var hsl = rgbToHsl(pixels[i + 0], pixels[i + 1], pixels[i + 2]);
var h = (hsl[0] + offset) % 360;
var s = hsl[1];
var l = hsl[2];
var rgb = hslToRgb(h, s, l);
pixels[i + 0] = rgb[0];
pixels[i + 1] = rgb[1];
pixels[i + 2] = rgb[2];
}
context.putImageData(imageData, 0, 0);
this._setDirty();
}
};
/**
* Applies a blur effect to the bitmap.
*
* @method blur
*/
Bitmap.prototype.blur = function() {
for (var i = 0; i < 2; i++) {
var w = this.width;
var h = this.height;
var canvas = this._canvas;
var context = this._context;
var tempCanvas = document.createElement('canvas');
var tempContext = tempCanvas.getContext('2d');
tempCanvas.width = w + 2;
tempCanvas.height = h + 2;
tempContext.drawImage(canvas, 0, 0, w, h, 1, 1, w, h);
tempContext.drawImage(canvas, 0, 0, w, 1, 1, 0, w, 1);
tempContext.drawImage(canvas, 0, 0, 1, h, 0, 1, 1, h);
tempContext.drawImage(canvas, 0, h - 1, w, 1, 1, h + 1, w, 1);
tempContext.drawImage(canvas, w - 1, 0, 1, h, w + 1, 1, 1, h);
context.save();
context.fillStyle = 'black';
context.fillRect(0, 0, w, h);
context.globalCompositeOperation = 'lighter';
context.globalAlpha = 1 / 9;
for (var y = 0; y < 3; y++) {
for (var x = 0; x < 3; x++) {
context.drawImage(tempCanvas, x, y, w, h, 0, 0, w, h);
}
}
context.restore();
}
this._setDirty();
};
/**
* Add a callback function that will be called when the bitmap is loaded.
*
* @method addLoadListener
* @param {Function} listner The callback function
*/
Bitmap.prototype.addLoadListener = function(listner) {
if (!this.isReady()) {
this._loadListeners.push(listner);
} else {
listner(this);
}
};
/**
* @method _makeFontNameText
* @private
*/
Bitmap.prototype._makeFontNameText = function() {
return (this.fontItalic ? 'Italic ' : '') +
this.fontSize + 'px ' + this.fontFace;
};
/**
* @method _drawTextOutline
* @param {String} text
* @param {Number} tx
* @param {Number} ty
* @param {Number} maxWidth
* @private
*/
Bitmap.prototype._drawTextOutline = function(text, tx, ty, maxWidth) {
var context = this._context;
context.strokeStyle = this.outlineColor;
context.lineWidth = this.outlineWidth;
context.lineJoin = 'round';
context.strokeText(text, tx, ty, maxWidth);
};
/**
* @method _drawTextBody
* @param {String} text
* @param {Number} tx
* @param {Number} ty
* @param {Number} maxWidth
* @private
*/
Bitmap.prototype._drawTextBody = function(text, tx, ty, maxWidth) {
var context = this._context;
context.fillStyle = this.textColor;
context.fillText(text, tx, ty, maxWidth);
};
/**
* @method _onLoad
* @private
*/
Bitmap.prototype._onLoad = function() {
this._image.removeEventListener('load', this._loadListener);
this._image.removeEventListener('error', this._errorListener);
this._renewCanvas();
switch(this._loadingState){
case 'requesting':
this._loadingState = 'requestCompleted';
if(this._decodeAfterRequest){
this.decode();
}else{
this._loadingState = 'purged';
this._clearImgInstance();
}
break;
case 'decrypting':
window.URL.revokeObjectURL(this._image.src);
this._loadingState = 'decryptCompleted';
if(this._decodeAfterRequest){
this.decode();
}else{
this._loadingState = 'purged';
this._clearImgInstance();
}
break;
}
};
Bitmap.prototype.decode = function(){
switch(this._loadingState){
case 'requestCompleted': case 'decryptCompleted':
this._loadingState = 'loaded';
if(!this.__canvas) this._createBaseTexture(this._image);
this._setDirty();
this._callLoadListeners();
break;
case 'requesting': case 'decrypting':
this._decodeAfterRequest = true;
if (!this._loader) {
this._loader = ResourceHandler.createLoader(this._url, this._requestImage.bind(this, this._url), this._onError.bind(this));
this._image.removeEventListener('error', this._errorListener);
this._image.addEventListener('error', this._errorListener = this._loader);
}
break;
case 'pending': case 'purged': case 'error':
this._decodeAfterRequest = true;
this._requestImage(this._url);
break;
}
};
/**
* @method _callLoadListeners
* @private
*/
Bitmap.prototype._callLoadListeners = function() {
while (this._loadListeners.length > 0) {
var listener = this._loadListeners.shift();
listener(this);
}
};
/**
* @method _onError
* @private
*/
Bitmap.prototype._onError = function() {
this._image.removeEventListener('load', this._loadListener);
this._image.removeEventListener('error', this._errorListener);
this._loadingState = 'error';
};
/**
* @method _setDirty
* @private
*/
Bitmap.prototype._setDirty = function() {
this._dirty = true;
};
/**
* updates texture is bitmap was dirty
* @method checkDirty
*/
Bitmap.prototype.checkDirty = function() {
if (this._dirty) {
this._baseTexture.update();
var baseTexture = this._baseTexture;
setTimeout(function() {
baseTexture.update();
}, 0);
this._dirty = false;
}
};
Bitmap.request = function(url){