-
Notifications
You must be signed in to change notification settings - Fork 5
/
rpg_core.js
7956 lines (7290 loc) · 193 KB
/
rpg_core.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
//=============================================================================
// rpg_core.js v1.1.0
//=============================================================================
//-----------------------------------------------------------------------------
/**
* This is not a class, but contains some methods that will be added to the
* standard Javascript objects.
*
* @class JsExtensions
*/
//js扩展类
function JsExtensions() {
throw new Error('This is not a class');
}
/**
* Returns a number whose value is limited to the given range.
*限制一个数的范围在min 和max之间
* @method Number.prototype.clamp
* @param {Number} min The lower boundary
* @param {Number} max The upper boundary
* @return {Number} A number in the range (min, max)
*/
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
/**
* Returns a modulo value which is always positive.
* @param {Number} n The divisor
*
* @method Number.prototype.mod
* @return {Number} A modulo value
*/
Number.prototype.mod = function(n) {
return ((this % n) + n) % n;
};
/**
* Replaces %1, %2 and so on in the string to the arguments.
*
* @method String.prototype.format
* @param {Any} ...args The objects to format
* @return {String} A formatted string
*/
String.prototype.format = function() {
var args = arguments;
return this.replace(/%([0-9]+)/g, function(s, n) {
return args[Number(n) - 1];
});
};
/**
* Makes a number string with leading zeros.
*
* @method String.prototype.padZero
* @param {Number} length The length of the output string
* @return {String} A string with leading zeros
*/
String.prototype.padZero = function(length){
var s = this;
while (s.length < length) {
s = '0' + s;
}
return s;
};
/**
* Makes a number string with leading zeros.
*
* @method Number.prototype.padZero
* @param {Number} length The length of the output string
* @return {String} A string with leading zeros
*/
Number.prototype.padZero = function(length){
return String(this).padZero(length);
};
/**
* Checks whether the two arrays are same.
*
* @method Array.prototype.equals
* @param {Array} array The array to compare to
* @return {Boolean} True if the two arrays are same
*/
Array.prototype.equals = function(array) {
if (!array || this.length !== array.length) {
return false;
}
for (var i = 0; i < this.length; i++) {
if (this[i] instanceof Array && array[i] instanceof Array) {
if (!this[i].equals(array[i])) {
return false;
}
} else if (this[i] !== array[i]) {
return false;
}
}
return true;
};
/**
* Makes a shallow copy of the array.
*
* @method Array.prototype.clone
* @return {Array} A shallow copy of the array
*/
Array.prototype.clone = function() {
return this.slice(0);
};
/**
* Checks whether the array contains a given element.
*
* @method Array.prototype.contains
* @param {Any} element The element to search for
* @return {Boolean} True if the array contains a given element
*/
Array.prototype.contains = function(element) {
return this.indexOf(element) >= 0;
};
/**
* Checks whether the string contains a given string.
*
* @method String.prototype.contains
* @param {String} string The string to search for
* @return {Boolean} True if the string contains a given string
*/
String.prototype.contains = function(string) {
return this.indexOf(string) >= 0;
};
/**
* Generates a random integer in the range (0, max-1).
*
* @static
* @method Math.randomInt
* @param {Number} max The upper boundary (excluded)
* @return {Number} A random integer
*/
Math.randomInt = function(max) {
return Math.floor(max * Math.random());
};
//-----------------------------------------------------------------------------
/**
* The static class that defines utility methods.
*
* @class Utils
*/
function Utils() {
throw new Error('This is a static class');
}
/**
* The name of the RPG Maker. 'MV' in the current version.
*
* @static
* @property RPGMAKER_NAME
* @type String
* @final
*/
Utils.RPGMAKER_NAME = 'MV';
/**
* The version of the RPG Maker.
*
* @static
* @property RPGMAKER_VERSION
* @type String
* @final
*/
Utils.RPGMAKER_VERSION = "1.1.0";
/**
* Checks whether the option is in the query string.
*
* @static
* @method isOptionValid
* @param {String} name The option name
* @return {Boolean} True if the option is in the query string
*/
Utils.isOptionValid = function(name) {
return location.search.slice(1).split('&').contains(name);
};
/**
* Checks whether the platform is NW.js.
*检测当前平台是不是nw.js
* @static
* @method isNwjs
* @return {Boolean} True if the platform is NW.js
*/
Utils.isNwjs = function() {
//根据一些平台特征进行检测 (在nw.js平台上require是一个全局方法而process是一个全局对象)
return typeof require === 'function' && typeof process === 'object';
};
/**
* Checks whether the platform is a mobile device.
*检测当前平台是不是移动设备
* @static
* @method isMobileDevice
* @return {Boolean} True if the platform is a mobile device
*/
Utils.isMobileDevice = function() {
//正则匹配,列举了市面上常见的移动设备
var r = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
//通过navigator.userAgent进行匹配,navigator.userAgent会返回设备描述
return !!navigator.userAgent.match(r);
};
/**
* Checks whether the browser is Mobile Safari.
* 检测当前平台是不是MobileSafari
* @static
* @method isMobileSafari
* @return {Boolean} True if the browser is Mobile Safari
*/
Utils.isMobileSafari = function() {
var agent = navigator.userAgent;
return !!(agent.match(/iPhone|iPad|iPod/) && agent.match(/AppleWebKit/) &&
!agent.match('CriOS'));
};
/**
* Checks whether the browser is Android Chrome.
*
* @static
* @method isAndroidChrome
* @return {Boolean} True if the browser is Android Chrome
*/
Utils.isAndroidChrome = function() {
var agent = navigator.userAgent;
return !!(agent.match(/Android/) && agent.match(/Chrome/));
};
/**
* Checks whether the browser can read files in the game folder.
*检测是否可以读文件
* @static
* @method canReadGameFiles
* @return {Boolean} True if the browser can read files in the game folder
*/
Utils.canReadGameFiles = function() {
//这里采用的做法是取最后一个script元素的源地址,然后请求他
var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length - 1];
var xhr = new XMLHttpRequest();
try {
xhr.open('GET', lastScript.src);
xhr.overrideMimeType('text/javascript');
xhr.send();
return true;
} catch (e) {
return false;
}
};
/**
* Makes a CSS color string from RGB values.
* rgb分量[0,255]到css颜色表达式的转换
* @static
* @method rgbToCssColor
* @param {Number} r The red value in the range (0, 255)
* @param {Number} g The green value in the range (0, 255)
* @param {Number} b The blue value in the range (0, 255)
* @return {String} CSS color string
*/
Utils.rgbToCssColor = function(r, g, b) {
r = Math.round(r);
g = Math.round(g);
b = Math.round(b);
return 'rgb(' + r + ',' + g + ',' + b + ')';
};
//-----------------------------------------------------------------------------
/**
* The point class.
* 点类
*
* @class Point
* @constructor
* @param {Number} x The x coordinate
* @param {Number} y The y coordinate
*/
function Point() {
this.initialize.apply(this, arguments);
}
//直接继承了pixi的点类,然后没做什么改动
//不过pixi的类是有层级分布的,它直接给人搞成plain了,无语
Point.prototype = Object.create(PIXI.Point.prototype);
Point.prototype.constructor = Point;
Point.prototype.initialize = function(x, y) {
PIXI.Point.call(this, x, y);
};
/**
* The x coordinate.
*
* @property x
* @type Number
*/
/**
* The y coordinate.
*
* @property y
* @type Number
*/
//-----------------------------------------------------------------------------
/**
* The rectangle class.
* 矩形类,直接继承pixi对应的矩形
* @class Rectangle
* @constructor
* @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
* @param {Number} height The height of the rectangle
*/
function Rectangle() {
this.initialize.apply(this, arguments);
}
Rectangle.prototype = Object.create(PIXI.Rectangle.prototype);
Rectangle.prototype.constructor = Rectangle;
Rectangle.prototype.initialize = function(x, y, width, height) {
PIXI.Rectangle.call(this, x, y, width, height);
};
/**
* @static
* @property emptyRectangle
* @type Rectangle
* @private
*/
Rectangle.emptyRectangle = new Rectangle(0, 0, 0, 0);
/**
* The x coordinate for the upper-left corner.
*
* @property x
* @type Number
*/
/**
* The y coordinate for the upper-left corner.
*
* @property y
* @type Number
*/
/**
* The width of the rectangle.
*
* @property width
* @type Number
*/
/**
* The height of the rectangle.
*
* @property height
* @type Number
*/
//-----------------------------------------------------------------------------
/**
* 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);
}
Bitmap.prototype.initialize = function(width, height) {
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);
this._baseTexture = new PIXI.BaseTexture(this._canvas);
this._baseTexture.scaleMode = PIXI.scaleModes.NEAREST;
this._image = null;
this._url = '';
this._paintOpacity = 255;
this._smooth = false;
this._loadListeners = [];
this._isLoading = false;
this._hasError = false;
/**
* 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 = new Bitmap();
bitmap._image = new Image();
bitmap._image.src = url;
//绑定两个事件
bitmap._image.onload = Bitmap.prototype._onLoad.bind(bitmap);
bitmap._image.onerror = Bitmap.prototype._onError.bind(bitmap);
bitmap._url = url;
bitmap._isLoading = true;
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 = new PIXI.RenderTexture(width, height);
if (stage) {
renderTexture.render(stage);
stage.worldTransform.identity();
}
if (Graphics.isWebGL()) {
var gl = renderTexture.renderer.gl;
var webGLPixels = new Uint8Array(4 * width * height);
gl.bindFramebuffer(gl.FRAMEBUFFER, renderTexture.textureBuffer.frameBuffer);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, webGLPixels);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
var canvasData = context.getImageData(0, 0, width, height);
canvasData.data.set(webGLPixels);
context.putImageData(canvasData, 0, 0);
} else {
context.drawImage(renderTexture.textureBuffer.canvas, 0, 0);
}
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._isLoading;
};
/**
* Checks whether a loading error has occurred.
*
* @method isError
* @return {Boolean} True if a loading error has occurred
*/
Bitmap.prototype.isError = function() {
return this._hasError;
};
/**
* [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() {
return this._isLoading ? 0 : this._canvas.width;
},
configurable: true
});
/**
* [read-only] The height of the bitmap.
*
* @property height
* @type Number
*/
Object.defineProperty(Bitmap.prototype, 'height', {
get: function() {
return this._isLoading ? 0 : this._canvas.height;
},
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._smooth) {
this._baseTexture.scaleMode = PIXI.scaleModes.LINEAR;
} else {
this._baseTexture.scaleMode = PIXI.scaleModes.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();
}
};
/**
* 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 clear
* @param {Number} height The height of the rectangle to clear
* @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 clear
* @param {Number} height The height of the rectangle to clear
* @param {String} color1 The start color of the gradation
* @param {String} color2 The end color of the gradation
* @param {Boolean} vertical Whether it draws a vertical gradient
*/
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 the filled circle.
*
* @method drawCircle
* @param {Number} x The x coordinate of the center of the circle
* @param {Number} y The y coordinate of the center of the circle
* @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) {
var tx = x;
var ty = y + lineHeight - (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();
}
};
/**
* 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);