我已经创建了我在 IONIC 中尝试做的事情的代码笔。
http://codepen.io/anon/pen/yNjmoK
HTML:
<html ng-app="myApp">
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/camanjs/4.0.0/caman.full.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
</head>
<body ng-controller="myCtrl">
<div id="photo">
<canvas id="canvas" width="400" height="400" style="border:1px solid #d3d3d3;"></canvas>
</div>
<div id="filterContainer">
<ul id="filters">
<li> <a href="#" id="normal" ng-click="applyFilter($event)">Normal</a> </li>
<li> <a href="#" id="vintage" ng-click="applyFilter($event)">Vintage</a> </li>
<li> <a href="#" id="lomo" ng-click="applyFilter($event)">Lomo</a> </li>
<li> <a href="#" id="clarity" ng-click="applyFilter($event)">Clarity</a> </li>
</div>
</body>
</html>
JS:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var image = new Image();
image.onload = function() {
context.drawImage(image, 0, 0, 460, 460);
};
image.crossOrigin = "anonymous";
image.src = "https://dl.dropboxusercontent.com/u/139992952/stackoverflow/colorhouse.png";
$scope.applyFilter = function(event) {
// Clone the canvas
var clone = canvas.cloneNode(true);
// Clone the image stored in the canvas as well
clone.getContext('2d').drawImage(canvas, 0, 0, 400, 400);
var theParent = document.getElementById("photo");
theParent.removeChild(document.getElementById('canvas'));
theParent.appendChild(clone);
var effect = String(event.target.id).trim();
Caman(clone, function() {
// If such an effect exists, use it:
if (effect in this) {
console.log("Effect GOOD");
this[effect]();
this.render();
} else {
console.log("Effect ERROR");
}
});
};
});
在 codepen 中引入了一个图像,我应用了一个过滤器。在 codepen 内部,这是有效的。然后我尝试将其应用于离子,但它不起作用。当我单击过滤器时,它会一直到达 caman 函数中的代码“if(效果在此)”,并在控制台中打印“效果很好”,但在电话上,画布变为白色,仅此而已。
更新:我刚刚在我的 android 设备上注意到,如果我从我的移动浏览器(chrome)运行 codepen,它就不起作用。如果我在桌面上的 chrome 中运行它,它确实可以工作。所以它看起来像一个浏览器问题?有没有什么办法解决这一问题?