简体   繁体   English

将图像过滤器应用于画布图像数据或JavaScript

[英]Applying image filters to canvas image data or in JavaScript

I'm a Flash developer, and I'm working on a JavaScript port of a game. 我是Flash开发人员,我正在开发游戏的JavaScript端口。 In the Flash version, I use glow, blur and color matrix filters to manipulate the appearances of display objects. 在Flash版本中,我使用发光,模糊和颜色矩阵滤镜来操纵显示对象的外观。 I would like to do the same with my JavaScript view. 我想用我的JavaScript视图做同样的事情。 Some of these are embellishments, while others are used to carefully produce a desired result. 其中一些是装饰,而另一些则用于仔细产生所需的结果。 Because this project's goals are to create an exact port, I'm wondering what options I have to apply filters to raw canvas data, to inline SVG tags or to straight up DOM elements. 因为这个项目的目标是创建一个精确的端口,我想知道我有什么选择将过滤器应用于原始画布数据,内联SVG标签或直接DOM元素。

I've considered Pixastic, but my collaborator insists on a GPL license for the time being, which means any tech I use must be GPL compatible. 我考虑过Pixastic,但我的合作者暂时坚持使用GPL许可证,这意味着我使用的任何技术都必须兼容GPL。 Pixastic uses the Mozilla Public License, so I can't use it. Pixastic使用Mozilla Public License,因此我无法使用它。 (Which is a huge bummer, let me tell you.) (这是一个巨大的失败,让我告诉你。)

I'll say it again concisely: how do I efficiently apply pixel filters to DOM elements, to canvas image data or to SVG tags with JavaScript? 我将再次简明扼要地说:如何有效地将像素滤镜应用于DOM元素,使用JavaScript绘制图像数据或SVG标签?

Here's an example showing some svg filtering: 这是一个显示一些svg过滤的示例:

and the corresponding canvas version: 和相应的画布版本:

Here are some js libraries for canvas doing what I think you're looking for: 这里有一些用于canvas的js库做我认为你正在寻找的东西:

A number of svg filter examples can be found on http://svg-wow.org (CC0 licensed). 许多svg过滤器示例可以在http://svg-wow.org (CC0许可)上找到。

I have created a library ( context-blender ) for performing Photoshop-style blend effects between HTML Canvases. 我创建了一个库( context-blender ),用于在HTML画布之间执行Photoshop风格的混合效果。 This isn't exactly what you need, as you want some convolution filters to run on the pixels other than the original, but the code path will be the same: getImageData() , munge the data, putImageData . 这不是您所需要的,因为您希望某些卷积滤镜在原始像素以外的像素上运行,但代码路径将是相同的: getImageData() ,munge数据, putImageData

My library happens to be MIT License, so feel free to investigate there with no fear of issues. 我的图书馆恰好是麻省理工学院的许可证,所以请随时调查,不要担心问题。

Filter.js library for image processing (including many AS3 filter types, like convolution, colormatrix, displacement map etc..) 用于图像处理的Filter.js库(包括许多AS3过滤器类型,如卷积,色矩阵,置换贴图等)。

https://github.com/foo123/FILTER.js https://github.com/foo123/FILTER.js

PS i am the author PS我是作者

The best way to filter images in Javascript is through an image processing framework. 在Javascript中过滤图像的最佳方法是通过图像处理框架。 Some pure Javascript options: 一些纯Javascript选项:

In the case of MarvinJ , use the following code to load your image: 对于MarvinJ ,请使用以下代码加载图像:

var image = new MarvinImage();
image.load("https://i.imgur.com/By6tvip.jpg", imageLoaded);

I'll use the following input image to demonstrate some filters: 我将使用以下输入图像来演示一些过滤器:

在此输入图像描述

GrayScale: 灰度:

 Marvin.grayScale(image, imageOut);

在此输入图像描述

Black and White: 黑和白:

Marvin.blackAndWhite(image, imageOut, 10);

在此输入图像描述

Sepia: 棕褐色:

Marvin.sepia(image, imageOut, 40);

在此输入图像描述

Emboss: 浮雕:

Marvin.emboss(image, imageOut);

在此输入图像描述

Edge Detection: 边缘检测:

Marvin.prewitt(image, imageOut);

在此输入图像描述

Blur: 模糊:

Marvin.gaussianBlur(image, imageOut, 3);

在此输入图像描述

Brightness and Contrast: 亮度和对比度:

Marvin.brightnessAndContrast(image, imageOut, 70, 60);

在此输入图像描述

Color Channel: 色彩通道:

Marvin.colorChannel(image, imageOut, 0, 0, 110);

在此输入图像描述

Runnable example of the previous filters: 以前过滤器的可运行示例:

 var canvas1 = document.getElementById("canvas1"); var image = new MarvinImage(); image.load("https://i.imgur.com/By6tvip.jpg", imageLoaded); function imageLoaded(){ var imageOut = new MarvinImage(image.getWidth(), image.getHeight()); // GrayScale Marvin.grayScale(image, imageOut); imageOut.draw(canvas1); // Black and White Marvin.blackAndWhite(image, imageOut, 10); imageOut.draw(canvas2); // Sepia Marvin.sepia(image, imageOut, 40); imageOut.draw(canvas3); // Emboss Marvin.emboss(image, imageOut); imageOut.draw(canvas4); // Edge imageOut.clear(0xFF000000); Marvin.prewitt(image, imageOut); imageOut.draw(canvas5); // Gaussian Blur Marvin.gaussianBlur(image, imageOut, 5); imageOut.draw(canvas6); // Brightness Marvin.brightnessAndContrast(image, imageOut, 70, 60); imageOut.draw(canvas7); // Color Channel Marvin.colorChannel(image, imageOut, 0, 0, 110); imageOut.draw(canvas8); } 
 <script src="https://www.marvinj.org/releases/marvinj-0.7.js"></script> <canvas id="canvas1" width="192" height="120"></canvas> <canvas id="canvas2" width="192" height="120"></canvas> <canvas id="canvas3" width="192" height="120"></canvas> <canvas id="canvas4" width="192" height="120"></canvas> <canvas id="canvas5" width="192" height="120"></canvas> <canvas id="canvas6" width="192" height="120"></canvas> <canvas id="canvas7" width="192" height="120"></canvas> <canvas id="canvas8" width="192" height="120"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM