From 3f2cdbd8f000f643dda25beec9f3b289184d3ec0 Mon Sep 17 00:00:00 2001
From: knoxHuang <821686174@qq.com>
Date: Mon, 1 Aug 2016 18:30:42 +0800
Subject: [PATCH] add enableAntiAlias for CCView fireball/issues/2836
---
cocos2d/core/platform/CCView.js | 53 ++++++++++++++++++++++++++++
cocos2d/core/textures/CCTexture2D.js | 10 ++++++
2 files changed, 63 insertions(+)
diff --git a/cocos2d/core/platform/CCView.js b/cocos2d/core/platform/CCView.js
index d8cb4cf851..f04fcc921a 100644
--- a/cocos2d/core/platform/CCView.js
+++ b/cocos2d/core/platform/CCView.js
@@ -150,6 +150,8 @@ var View = cc._Class.extend({
_isAdjustViewPort: true,
_targetDensityDPI: null,
+ _antiAliasEnabled: false,
+
/**
* Constructor of View
*/
@@ -181,6 +183,7 @@ var View = cc._Class.extend({
_t._rpFixedWidth = new cc.ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.FIXED_WIDTH);
_t._targetDensityDPI = cc.macro.DENSITYDPI_HIGH;
+ _t.enableAntiAlias(true);
},
// Resize helper functions
@@ -415,6 +418,56 @@ var View = cc._Class.extend({
return this._retinaEnabled;
},
+ /**
+ * !#en Whether to Enable on anti-alias
+ * !#zh 是否开启抗锯齿
+ * @method enableAntiAlias
+ * @param {Boolean} enabled - Enable or not anti-alias
+ */
+ enableAntiAlias: function (enabled) {
+ if (this._antiAliasEnabled === enabled) {
+ return;
+ }
+ this._antiAliasEnabled = enabled;
+ if(cc._renderType === cc.game.RENDER_TYPE_WEBGL) {
+ var map = cc.loader._items.map;
+ for (var key in map) {
+ var item = map[key];
+ var tex = item && item.content instanceof cc.Texture2D ? item.content : null;
+ if (tex) {
+ if (enabled) {
+ tex.setAntiAliasTexParameters();
+ }
+ else {
+ tex.setAliasTexParameters();
+ }
+ }
+ }
+ }
+ else if(cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
+ var ctx = cc._canvas.getContext('2d');
+ ctx.imageSmoothingEnabled = enabled;
+ ctx.mozImageSmoothingEnabled = enabled;
+ // refresh canvas
+ var dirtyRegion = cc.rendererCanvas._dirtyRegion;
+ if (dirtyRegion) {
+ var oldRegion = new cc.Region();
+ oldRegion.setTo(0, 0, cc.visibleRect.width, cc.visibleRect.height);
+ dirtyRegion.addRegion(oldRegion);
+ }
+ }
+ },
+
+ /**
+ * !#en Returns whether the current enable on anti-alias
+ * !#zh 返回当前是否抗锯齿
+ * @method isAntiAliasEnabled
+ * @param {Boolean}
+ */
+ isAntiAliasEnabled: function () {
+ return this._antiAliasEnabled;
+ },
+
/**
* If enabled, the application will try automatically to enter full screen mode on mobile devices
* You can pass true as parameter to enable it and disable it by passing false.
diff --git a/cocos2d/core/textures/CCTexture2D.js b/cocos2d/core/textures/CCTexture2D.js
index 3f6a17b485..7f1e7cf93b 100644
--- a/cocos2d/core/textures/CCTexture2D.js
+++ b/cocos2d/core/textures/CCTexture2D.js
@@ -233,6 +233,16 @@ var Texture2D = cc.Class(/** @lends cc.Texture2D# */{
self._pixelHeight = self._contentSize.height = locElement.height;
self._textureLoaded = true;
+ // anti-alias
+ if(cc._renderType === cc.game.RENDER_TYPE_WEBGL) {
+ if (cc.view._antiAliasEnabled) {
+ self.setAntiAliasTexParameters();
+ }
+ else {
+ self.setAliasTexParameters();
+ }
+ }
+
//dispatch load event to listener.
self.emit("load");
},