HTML5 canvas生成图片马赛克特效插件
HTML5 canvas生成图片马赛克特效插件
简要教程
这是一款使用html5 canvas来将图片制作成马赛克效果的js插件。该插件的灵感来自于美国肖像画家Chuck Close。已经有人使用这个js插件来制作了一个完整的图片马赛克应用框架:The Pixelator。
使用方法
这个马赛克效果js插件需要使用同源的图片,根据 HTML5 规范,浏览器在解析getImageData()方法时会阻止获取跨浏览器的图片。
该canvas插件的html结构非常简单,就是使用一个<img>标签:
<img id="portrait-image" src="img/portrait.jpg" /> 然后可以使用下面的方法调用插件:
function init() {
document.getElementById('portrait-image').closePixelate([
{ resolution : },
{ shape : 'circle', resolution : , size: , offset: , alpha: 0.5 }
]);
};
window.addEventListener( 'load', init, false); 你可以使用一个可用参数数组中来控制输出的马赛克效果。参数数组中每一个集合都是一个对象,代表了某种马赛克输出效果。
在上面的例子中,第一组参数{ resolution : }控制脚本每24像素就画一个大的正方形像素,然后为每一个正方形像素的中心填充一个精确的颜色值。
第二组参数使用了所有的可用参数:{ shape : 'circle', resolution : , size: , offset: , alpha: 0.5 }。和第一组参数一样,它的分辨率是24px,像素的形状是圆形,每一个圆形的大小是16像素,每一个圆形的右边都有12像素的偏移量,最后这个圆形的透明度被设置为50%。
可用参数
resolution :被渲染像素之间的距离。必须设定。
shape :像素的形状。可选值:square、circle 和 diamond,默认值:square。可选。
size :渲染像素的大小。可选。默认值为resolution。
offset :像素之间的偏移值。可选,默认值为0.可以只设置一个值,这时为对角偏移。也可以设置为一个数组或对象:[ , ]或{ x: , y: }。
alpha :渲染像素的透明度,可选,默认值为1。
构造函数和方法
var img = document.getElementById('portrait-img');
// create a new Close Pixelation instance with ClosePixelation
// requires two arguments: the original image element
// and an array of options
var myPixelation = new ClosePixelation( img, [
{ resolution : }
]);
// re-render the canvas with different options
myPixelation.render([
{ resolution: },
{ resolution: , shape: 'circle', offset: }
]);
// render a single option-set on top
myPixelation.renderClosePixels({
resolution: , alpha: 0.5
}); 应用举例
html5 canvas图片马赛克效果-
{ shape : 'diamond', resolution : , size: },
{ shape : 'diamond', resolution : , offset : },
{ shape : 'circle', resolution : , size: } html5 canvas图片马赛克效果-
{ resolution: },
{ shape : 'circle', resolution : , offset: },
{ shape : 'circle', resolution : , size: , offset: },
{ shape : 'circle', resolution : , size: , offset: },
{ shape : 'circle', resolution : , size: , offset: } html5 canvas图片马赛克效果-
{ resolution: },
{ shape: 'diamond', resolution: , offset: , alpha: 0.5 },
{ shape: 'diamond', resolution: , offset: , alpha: 0.5 },
{ shape: 'circle', resolution: , size: , offset: , alpha: 0.5 } html5 canvas图片马赛克效果-
{ shape: 'circle', resolution: , size: , offset: },
{ shape: 'circle', resolution: , size: , offset: },
{ shape: 'circle', resolution: , size: , offset: },
{ shape: 'circle', resolution: , size: , offset: } html5 canvas图片马赛克效果-
{ shape: 'diamond', resolution: , size: },
{ shape: 'diamond', resolution: , offset: },
{ resolution: , alpha: 0.5 } html5 canvas图片马赛克效果-
{ shape: 'square', resolution: },
{ shape: 'circle', resolution: , offset: },
{ shape: 'circle', resolution: , offset: , alpha: 0.5 },
{ shape: 'circle', resolution: , size: , offset: , alpha: 0.5 } html5 canvas图片马赛克效果-
{ shape : 'circle', resolution : },
{ shape : 'circle', resolution : , size: , offset: } html5 canvas图片马赛克效果-
{ shape : 'square', resolution : , offset: },
{ shape : 'circle', resolution : , offset : },
{ shape : 'diamond', resolution : , size: , offset : , alpha : 0.6 },
{ shape : 'diamond', resolution : , size: , offset : , alpha : 0.6 } html5 canvas图片马赛克效果-
{ shape : 'square', resolution : },
{ shape : 'diamond', resolution : , size: },
{ shape : 'diamond', resolution : , size: , offset : } 本文地址:http://www.htmleaf.com/html5/html5-canvas/201502021314.html
HTML5 canvas生成图片马赛克特效插件的更多相关文章
- 基于html5 canvas 的强大图表插件【Chart.js】
名词解释 Chart.js:是基于html5和canvas的强大图表插件,支持多样的图表形式,柱状线性饼环极地雷达等等: canvas:只兼容到IE9 excanvas.js:强大的第三方兼容插件,可 ...
- html5 canvas做的图表插件
用highchart的时候发现它是用svg来画图的,那么用canvas来做怎么样的. 以前做AS图表插件的时候,绘制图画主要用容器的Graphics对象来绘制,而canvas的context和Grap ...
- 黄聪:手机移动端建站Jquery+CSS3+HTML5触屏滑动特效插件、实现触屏焦点图、图片轮展图
前言 TouchSlide 可以说是 SuperSlide 手机简化版,不同的地方在于:1.TouchSlide是纯javascript开发的,不依赖任何js库,鉴于此,TouchSlide调用方法和 ...
- html5 canvas图片马赛克
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 程序猿必备的10款超炫酷HTML5 Canvas插件
1.超炫酷HTML5 Canvas 3D旋转地球动画 这是一款基于HTML5 Canvas的3D地球模拟动画,动画以太空作为背景,地球在太空中旋转,同时我们也可以拖拽鼠标来从不同的角度观察地球.另外我 ...
- 16个富有创意的HTML5 Canvas动画特效集合
HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...
- 16个非常有趣的HTML5 Canvas动画特效集合
HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...
- 基于HTML5 Canvas粒子效果文字动画特效
之前我们分享过很多超酷的文字特效,其中也有利用HTML5和CSS3的.今天我们要来分享一款基于HTML5 Canvas的文字特效,输入框中输入想要展示的文字,回车后即可在canvas上绘制出粒子效果的 ...
- 一款由html5 canvas实现五彩小圆圈背景特效
之前介绍了好几款html5 canvas实现的特效.今天要为大家介绍一款由html5 canvas实现五彩小圆圈背景特效.五彩的小圆圈渐显渐失的特效.效果图如下: 在线预览 源码下载 html代码 ...
随机推荐
- 【模拟】Vijos P1005 超长数字串
题目链接: https://vijos.org/p/1005 题目大意: 无限的正整数按顺序拼接成字符串S(S=12345678910111213...),给你一个字符串A(len<=200)求 ...
- wp8模拟器中使用电脑键盘和模拟器的版本解释
一:如图启用和关闭 二:电脑按键对应的手机的按键 三:模拟器的版本解释
- PostgreSQL安装详细步骤(windows)
原文地址:http://blog.chinaunix.net/uid-354915-id-3498734.html PostgreSQL安装:一.windows下安装过程安装介质:postgresql ...
- 在Apache+php中使用json来通讯
示例代码: <?php // 获取输入的内容 $request = http_get_request_body(); // 按json格式解析成一个 php对象 $json_obj = json ...
- 用Processon在线绘制UML的尝试
地址https://www.processon.com/ ProcessOn是一个面向垂直专业领域的作图工具和社交网络,成立于2011年6月并于2012年启动.ProcessOn将全球的专家顾问.咨询 ...
- Safecracker(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1015 / 题意; 从所给的一串字符串中选出5个字母假如是(A B C D E)使得A-B^2+C^3-D^4+E ...
- [TypeScript] Inheritance
Inheritance is a way toindicate that a class receives behavior from a parent class. Then we can over ...
- CSU1661: Query Mutiple
Description One day,Little-Y saw many numbers standing in a row. A question suddenly appeared in her ...
- Android源代码分析之Framework的MediaPlayer
在Android中MediaPlayer用来播放音频和视频文件,在这里分析下在Framework层中MediaPlayer是怎样调用的.MediaPlayer的代码位于:./frameworks/ba ...
- The builder launch configuration could not be found
Export Wizard Error Errors occurred during the build Problems occured when invoking code from p ...