<!DOCTYPE html>
<html lang='zh-cn'>
<head>
<title>Insert you title</title>
<meta name='description' content='this is my page'>
<meta name='keywords' content='keyword1,keyword2,keyword3'>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel='stylesheet' type='text/css' href='./css/index.css' />
<script type='text/javascript' src='./js/jquery-1.12.1.min.js'></script>
<style type='text/css'>
html,body,canvas {
margin: 0; padding: 0;
} html {
height: 100%;
} body {
background: #000;
} #can {
background: #FFF /* url('./images/index.jpg') no-repeat center; backgrond-size: cover */;
display: block; margin: 25px auto; border-radius: 2px; cursor: pointer;
}
</style>
<script type='text/javascript'>
$( function(){
var can = $( '#can' ).get( 0 );
var oCan = can.getContext( '2d' );
var oImg = new Image();
oImg.src = './images/index.jpg';
var gImg = oCan.getImageData( 50 , 30 , 100 , 150 ); /* 之所以进行获取,是为了循环中读取个数 */
oImg.onload = function(){
var num = 0;
var length = gImg.data.length;
var img = oCan.createImageData( 100 , 100 );
for( var i = 0 ; i < length ; i++ ){
num = i % 4; /* 0 1 2 3 */
switch( num ){
case 0 :
num = 0;
img.data[ i ] = getRandomNum();
break;
case 1 :
num = 255;
img.data[ i ] = getRandomNum();
break;
case 2 :
num = 0;
img.data[ i ] = getRandomNum();
break;
case 3 :
num = getRandomNum();
img.data[ i ] = num;
break;
}
}
oCan.putImageData( img , 10 , 20 );
};
function getRandomNum(){
return Math.ceil( Math.random() * 255 ); /* 0-- 255 */
}
//alert(getRandomNum());
} );
</script>
</head>
<body>
<canvas id='can' width='300' height='168'>检测到您的浏览器版本过低请升级您的浏览器版本以获取更好的用户体验...</canvas>
</body>
</html>
05-11 09:31
查看更多