使用CamanJS进行图像处理

时间:2015-08-30 10:29:48

标签: javascript html css image camanjs

我找到了一个名为CamanJS的图像处理库,示例看起来不错,所以我尝试使用它。我得到了以下HTML:

<ion-view class="menu-content" view-title="Filter">
    <ion-content overflow-scroll="true">
        <div id="polaroid-filter-img" class="polaroid-filter-img">
            <img id="polaroid-filter-img-img" ng-src="{{polaroid.cropped}}" />
        </div>
        <div class="polaroid-filter-examples">
            <div ng-repeat="filter in filters" ng-click="performFilter(filter.id)" id="filter.id">
                <img id="{{filter.id}}" ng-src="{{filter.image}}"/>
                <span>{{filter.name}}</span>
            </div>
        </div>
    </ion-content>
</ion-view>

这个css:

.polaroid-filter-img {
    display: block;
    position: relative;
    margin-left: auto;
    margin-right: auto;
    top: 10%;
}

.polaroid-filter-img img {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
}

@media only screen and (orientation: portrait) {
    .polaroid-filter-examples {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        overflow: auto;
        overflow-x: scroll;
        overflow-y: hidden;
        white-space: nowrap;
    }
    .polaroid-filter-examples div {
        display: inline-block;
    }
}

@media only screen and (orientation: landscape) {
    .polaroid-filter-examples {
        position: fixed;
        top: 0;
        right: 0;
        height: 100%;
        overflow: auto;
        overflow-x: hidden;
        overflow-y: scroll;
        white-space: nowrap;
    }
    .polaroid-filter-examples div {
        display: block;
    }
}

.polaroid-filter-examples div {
    font-size: 14px;
    text-align: center;
    color: white;
}

.polaroid-filter-examples img {
    margin: 0 auto;
    padding: 2px 2px 2px 2px;
    width: 125px;
    height: 125px;
}

.polaroid-filter-examples span {
    display: block;
    padding-bottom: 10px;
    padding-top: 10px;
}

这个JavaScript:

angular.module("App.Polaroids-Edit-Filter")

.controller("PolaroidsEditFilterController", function ($scope, $stateParams, PolaroidsService, FiltersService) {
    $scope.polaroid = PolaroidsService.getPolaroid($stateParams.id, $stateParams.size);
    $scope.filters = FiltersService.getFilters();

    var previousFilter = null;

    $scope.performFilter = function (id) {
        if (id != null && id != previousFilter) {
            if (previousFilter != null) {
                document.getElementById(previousFilter).style.border = "none";
            }

            document.getElementById(id).style.border = "solid #FF0000";

            if (id == "normal") {
                var image = document.getElementById("polaroid-filter-img-img");

                img.src = $scope.polaroid.cropped;
            } else {
                var image = document.getElementById("polaroid-filter-img-img");

                Caman(image, function () {
                    if (id == "vintage") {
                        this.vintage();
                    }

                    this.render(function () {
                        console.log("filtering finished");
                    });
                });   
            }

            previousFilter = id;
        }
    };

    function initView() {
        var width = screen.width * 0.7;
        var height = width;
        var initialId = $scope.filters[0].id;

        previousFilter = initialId;

        document.getElementById("polaroid-filter-img").style.width = width + "px";
        document.getElementById("polaroid-filter-img").style.height = height + "px";
        document.getElementById(initialId).style.border = "solid #FF0000";
    }

    var intervalId = setInterval(function () {
        initView();

        window.clearInterval(intervalId);
    }, 100);
});

当我启动我的应用程序并选择图片时,我得到以下内容:

enter image description here

当我现在点击老式过滤器时,会发生以下情况:

enter image description here

正如您所看到的,图像被调整为实际宽度和高度以及之后 大约20秒:

enter image description here

过滤器被应用。所以我有两种奇怪的行为,我不明白。

  • 第一:为什么图像会调整大小?
  • 第二:为什么需要这么长时间(20秒)来应用过滤器?我找到了一个他们使用CamanJS的演示,在该演示中,过滤几乎立即执行:Instagram Filters

你能帮我理解那里出了什么问题吗?

修改

如果我使用如here所示的画布,那么在应用滤镜时画布就会消失。

2 个答案:

答案 0 :(得分:1)

我检查了您的代码,发现了1个与CSS有关的小问题。在您的班级.polaroid-filter-img img中,有position: absolute;。这应该是position: relative;。例如:

.polaroid-filter-img img {
    position: relative;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
}

我想到的另一个问题是你的函数initView中的javascript。在此函数中,您的以下值出错heightwidth

    var width = screen.width * 0.7;
    var height = width;
    var initialId = $scope.filters[0].id;

请在控制台日志

中查看heightwidth

答案 1 :(得分:1)

如果它仍然可以帮助您解决问题,请查看其他文章Angularjs code works in desktop chrome, but not in mobile chrome 解决方案很好地适用于Ionic + AngularJS + Phonegap,没有奇怪的大小调整。 HTH。

相关问题