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

发布于 2024-10-11 04:26:03 字数 396 浏览 1 评论 0原文

我是一名 Flash 开发人员,正在开发游戏的 JavaScript 端口。在 Flash 版本中,我使用发光、模糊和颜色矩阵滤镜来操纵显示对象的外观。我想对我的 JavaScript 视图做同样的事情。其中一些是装饰,而另一些则用于仔细地产生期望的结果。因为这个项目的目标是创建一个精确的端口,所以我想知道我必须使用哪些选项来将过滤器应用于原始画布数据、内联 SVG 标签或直接 DOM 元素。

我考虑过 Pixastic,但我的合作者暂时坚持使用 GPL 许可证,这意味着我使用的任何技术都必须兼容 GPL。 Pixastic 使用 Mozilla 公共许可证,所以我无法使用它。 (这是一个巨大的无赖,让我告诉你。)

我会简洁地再说一遍:如何使用 JavaScript 有效地将像素过滤器应用于 DOM 元素、画布图像数据或 SVG 标签?

I'm a Flash developer, and I'm working on a JavaScript port of a game. In the Flash version, I use glow, blur and color matrix filters to manipulate the appearances of display objects. I would like to do the same with my JavaScript view. 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.

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 uses the Mozilla Public License, so I can't use it. (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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月下客 2024-10-18 04:26:03

以下是显示一些 svg 过滤的示例:

和相应的画布版本:

这里有一些画布的 js 库,可以做我认为你正在寻找的事情:

许多 svg 过滤器示例可以在 http://svg-wow.org(CC0 许可)。

Here's an example showing some svg filtering:

and the corresponding canvas version:

Here are some js libraries for canvas doing what I think you're looking for:

A number of svg filter examples can be found on http://svg-wow.org (CC0 licensed).

百善笑为先 2024-10-18 04:26:03

我创建了一个库 (context-blender),用于在 HTML Canvas 之间执行 Photoshop 风格的混合效果。这并不完全是您所需要的,因为您希望一些卷积过滤器在原始像素以外的像素上运行,但代码路径将是相同的:getImageData(),munge the data,<代码>putImageData。

我的图书馆恰好是麻省理工学院的许可证,所以请随意调查那里,不用担心出现问题。

I have created a library (context-blender) for performing Photoshop-style blend effects between HTML Canvases. 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.

My library happens to be MIT License, so feel free to investigate there with no fear of issues.

提笔书几行 2024-10-18 04:26:03

用于图像处理的 Filter.js 库(包括许多 AS3 滤镜类型,如卷积、颜色矩阵、位移图等。)

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

PS我是作者

Filter.js library for image processing (including many AS3 filter types, like convolution, colormatrix, displacement map etc..)

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

PS i am the author

谷夏 2024-10-18 04:26:03

在 Javascript 中过滤图像的最佳方法是通过图像处理框架。一些纯 Javascript 选项:

MarvinJ,使用以下代码加载您的图像:

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

我将使​​用以下输入图像来演示一些过滤器:

在此处输入图像描述

灰度:

 Marvin.grayScale(image, imageOut);

在此处输入图像描述

黑白:

Marvin.blackAndWhite(image, imageOut, 10);

在此处输入图像描述

棕褐色:

Marvin.sepia(image, imageOut, 40);

在此处输入图像描述

浮雕:

Marvin.emboss(image, imageOut);

在此处输入图像描述

边缘检测:

Marvin.prewitt(image, imageOut);

在此处输入图像描述

模糊:

Marvin.gaussianBlur(image, imageOut, 3);

< a href="https://i.sstatic.net/ExNFa.png" rel="nofollow noreferrer">输入图像描述这里

亮度和对比度:

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

在此处输入图像描述

颜色通道:

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

在此处输入图像描述

可运行示例之前的过滤器:

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>

The best way to filter images in Javascript is through an image processing framework. Some pure Javascript options:

In the case of MarvinJ, use the following code to load your image:

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

I'll use the following input image to demonstrate some filters:

enter image description here

GrayScale:

 Marvin.grayScale(image, imageOut);

enter image description here

Black and White:

Marvin.blackAndWhite(image, imageOut, 10);

enter image description here

Sepia:

Marvin.sepia(image, imageOut, 40);

enter image description here

Emboss:

Marvin.emboss(image, imageOut);

enter image description here

Edge Detection:

Marvin.prewitt(image, imageOut);

enter image description here

Blur:

Marvin.gaussianBlur(image, imageOut, 3);

enter image description here

Brightness and Contrast:

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

enter image description here

Color Channel:

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

enter image description here

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文