<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
}
html,body{
width:100%;
height:100%;
}
</style>
</head>
<body>
<div id="canvas" style="width:500px;height:500px;position:relative;margin:0 auto;"></div>
</body>
</html>
<script>
var jquery = (function($){
var $ = function(id){
return document.getElementById(id) || id;
}
return $;
}());
var color = (function($){
var extend = function(target,source){
for(var x in source){
if(x in target){
target[x] = source[x];
}
}
return target;
}
var addEvent = function(obj,event,func){
obj.addEventListenter ? obj.addEventListenter(event,func) : obj.attachEvent("on"+event,function(){func.call(obj)});
}
window.requestAnimation = function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(fn){
return window.setTimeout(fn,1000/60);
}
}
/**
* 获取mimeType
* @param {String} type the old mime-type
* @return the new mime-type
*/
var _fixType = function(type) { //获取图片的mime类型
type = type.toLowerCase().replace(/jpg/i, 'jpeg');
var r = type.match(/png|jpeg|bmp|gif/)[0];
return 'image/' + r;
};
/**
* 在本地进行文件保存
* @param {String} data 要保存到本地的图片base64数据
* @param {String} filename 文件名
*/
var saveFile = function(data, filename){
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = data;
save_link.download = filename;
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
};
var init = function(opt){
this.option = {
element : null
}
extend(this.option,opt);
this.initialize();
}
init.prototype = {
initialize : function(){
this.canvas = this.createCanvas();
this.ctx = this.canvas.getContext("2d");
this.turnColor();
},
turnColor : function(){
var image = new Image(),_this = this;
image.src = "./1.jpg";
image.onload = function(){
$(_this.option.element).style.width = parseInt(image.width)+"px";
$(_this.option.element).style.height = parseInt(image.height)+"px";
_this.canvas.width = parseInt(image.width);
_this.canvas.height = parseInt(image.height);
_this.ctx.drawImage(image,0,0);
var imageData = _this.ctx.getImageData(0,0,image.width,image.height);
for(var i = 0,imageDataLeng = imageData.data.length;i < imageDataLeng;i += 4){
// First bytes are red bytes.
// Get red value.
var myRed = imageData.data[i];
// Second bytes are green bytes.
// Get green value.
var myGreen = imageData.data[i + 1];
// Third bytes are blue bytes.
// Get blue value.
var myBlue = imageData.data[i + 2];
// Fourth bytes are alpha bytes
// We don't care about alpha here.
// Add the three values and divide by three.
// Make it an integer.
myGray = parseInt((myRed + myGreen + myBlue) / 3);
// Assign average to red, green, and blue.
imageData.data[i] = myGray;
imageData.data[i + 1] = myGray;
imageData.data[i + 2] = myGray;
}
_this.ctx.putImageData(imageData,0,0);
// 图片导出为 png 格式
var type = 'png';
var imgData = _this.canvas.toDataURL(type); //获取canvas的图片base64数据
// 加工image data,替换mime type
imgData = imgData.replace(_fixType(type),'image/octet-stream');
// 下载后的问题名
var filename = '1.' + type;
// download
saveFile(imgData,filename);
}
},
createCanvas : function(){
var canvas = document.createElement("CANVAS");
canvas.innerHTML = "canvas";
canvas.width = parseInt($(this.option.element).style.width);
canvas.height = parseInt($(this.option.element).style.height);
$(this.option.element).appendChild(canvas);
return canvas;
}
}
return init;
}(jquery || {}));
window.onload = function(){
var option = {
element : "canvas"
}
new color(option);
}
</script>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
}
html,body{
width:100%;
height:100%;
}
</style>
</head>
<body>
<div id="canvas" style="width:500px;height:500px;position:relative;margin:0 auto;"></div>
</body>
</html>
<script>
var jquery = (function($){
var $ = function(id){
return document.getElementById(id) || id;
}
return $;
}());
var color = (function($){
var extend = function(target,source){
for(var x in source){
if(x in target){
target[x] = source[x];
}
}
return target;
}
var addEvent = function(obj,event,func){
obj.addEventListenter ? obj.addEventListenter(event,func) : obj.attachEvent("on"+event,function(){func.call(obj)});
}
window.requestAnimation = function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(fn){
return window.setTimeout(fn,1000/60);
}
}
/**
* 获取mimeType
* @param {String} type the old mime-type
* @return the new mime-type
*/
var _fixType = function(type) { //获取图片的mime类型
type = type.toLowerCase().replace(/jpg/i, 'jpeg');
var r = type.match(/png|jpeg|bmp|gif/)[0];
return 'image/' + r;
};
/**
* 在本地进行文件保存
* @param {String} data 要保存到本地的图片base64数据
* @param {String} filename 文件名
*/
var saveFile = function(data, filename){
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = data;
save_link.download = filename;
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
};
var init = function(opt){
this.option = {
element : null
}
extend(this.option,opt);
this.initialize();
}
init.prototype = {
initialize : function(){
this.canvas = this.createCanvas();
this.ctx = this.canvas.getContext("2d");
this.turnColor();
},
turnColor : function(){
var image = new Image(),_this = this;
image.src = "./1.jpg";
image.onload = function(){
$(_this.option.element).style.width = parseInt(image.width)+"px";
$(_this.option.element).style.height = parseInt(image.height)+"px";
_this.canvas.width = parseInt(image.width);
_this.canvas.height = parseInt(image.height);
_this.ctx.drawImage(image,0,0);
var imageData = _this.ctx.getImageData(0,0,image.width,image.height);
for(var i = 0,imageDataLeng = imageData.data.length;i < imageDataLeng;i += 4){
// First bytes are red bytes.
// Get red value.
var myRed = imageData.data[i];
// Second bytes are green bytes.
// Get green value.
var myGreen = imageData.data[i + 1];
// Third bytes are blue bytes.
// Get blue value.
var myBlue = imageData.data[i + 2];
// Fourth bytes are alpha bytes
// We don't care about alpha here.
// Add the three values and divide by three.
// Make it an integer.
myGray = parseInt((myRed + myGreen + myBlue) / 3);
// Assign average to red, green, and blue.
imageData.data[i] = myGray;
imageData.data[i + 1] = myGray;
imageData.data[i + 2] = myGray;
}
_this.ctx.putImageData(imageData,0,0);
// 图片导出为 png 格式
var type = 'png';
var imgData = _this.canvas.toDataURL(type); //获取canvas的图片base64数据
// 加工image data,替换mime type
imgData = imgData.replace(_fixType(type),'image/octet-stream');
// 下载后的问题名
var filename = '1.' + type;
// download
saveFile(imgData,filename);
}
},
createCanvas : function(){
var canvas = document.createElement("CANVAS");
canvas.innerHTML = "canvas";
canvas.width = parseInt($(this.option.element).style.width);
canvas.height = parseInt($(this.option.element).style.height);
$(this.option.element).appendChild(canvas);
return canvas;
}
}
return init;
}(jquery || {}));
window.onload = function(){
var option = {
element : "canvas"
}
new color(option);
}
</script>