使用HTML5中的 Canvas截图?

想了好久不知道怎么做~做一个 类似 PS的 截图的小工具~
使用 JS file 获取本地图片 与 Canvas 进行 截取~
但是 貌似 卡住了~ 代码~ 有兴趣的小伙伴 一起交流 跪舔 大神 QQ 1006775252~~~~
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<style>
.wire{
background: red;
width: 500px;
height:10px;
background:red;margin-bottom:50px;cursor:pointer;position:absolute;top:0px;left:0px;
}
</style>
</head>
<body >
<div style="position:relative;height:50px;" id="body">
<div class="wire" style="width:500px;height:10px;background:red;margin-bottom:50px;cursor:pointer;position:absolute;top:0px;left:0px;">
</div>
</div>
<div style="margin-bottom:80px;margin-top:50px;">
<label>请选择文件</label>
<input type="file" id="file">
<input type="button" value="读取图像" onclick="clic()">
</div>
<canvas id="can" style="width:400px;height:400px"></canvas>
<canvas id="a" style="width:400px;height:400px;display:none";> </canvas>
<button onclick="copy()">复制</button>
<script src="jquery-1.11.2.min.js"></script>
<script type="text/javascript">
if(typeof FileReader == 'undefined'){alert("您的浏览器不支持FileReader")}
var canvas = document.getElementById("can");
var can = document.getElementById("a");
var con = can.getContext("2d");
var context = canvas.getContext("2d");
function clic(){
var file = document.getElementById("file").files[0];
var reader = new FileReader();
if(!/image\/\w+/.test(file.type)){
alert("请选择图像");
}
reader.readAsDataURL(file);
function drew(){
var grd=context.createLinearGradient(0,0,170,0);
context.fillStyle=grd;
context.fillRect(0,0,400,300);
imaged = new Image();
reader.onload = function(){
imaged.src = this.result;
drawImg(context,imaged);
}
}
function drawImg(context,image){
// createClip(context);
context.drawImage(imaged,0,0);
}
drew()
}
function copy(){
$("#a").show();
var imgData = context.getImageData(0,0,100,30);
con.putImageData(imgData,0,0);
}
var i=1;
function drag(e,oWir,h){
e = e || window.event();
var clientX = e.clientX;
var clientY = e.clientY;
var DisY = clientY - oWir.offset().top;
$(document).on("mousemove",function(e){
oWir.css({
top: e.clientY - DisY +"px",
})
})
$(document).on("mouseup",function(e){
$(this).off("mousemove");
$(this).off("mouseup");
var objNewDiv = $('<div>',{'class':'wire'+h,});
$("#body").append(objNewDiv);
objNewDiv.css({
'background': 'red',
'width': '500px',
'height':'10px',
'cursor':'pointer',
'position':'absolute',
'top':'0px',
'left':'0px',
'MarginBottom':'50px'
})
})
}
$(".wire").on("mousedown",function(e){
drag(e,$(".wire"),1)
})
$("body").on("mousedown",".wire1",function(e){
drag(e,$(".wire1"),2)
})
$("body").on("mousedown",".wire2",function(e){
drag(e,$(".wire2"),3)
})
</script>
</body>
</html>
相关推荐
JavaScript+html5 canvas实现本地截图教程
最近有时间了解了下html5的各API,发现新浪微博的头像设置是使用canvas实现截图的,加之前段时间了解了下html5的File API使用File API 之FileReader实现文件上传《JavaScript File API文件上传预览》,更加觉得html5好玩了,想着也试试写写这功能权当学习canvas吧。 下面奉上我自己写的一个demo,代码写得比较少,很多细节不会处理。如果有不得当的地方恳请指教,谢谢啦 ^_^ ^_^ 功能实现步奏: 一、获取文件,读取文件并生成url 二、根据容器的大小使用canvas绘制图片 三、使用canvas绘制遮罩层
HTML5摄像头播放视频和截图
基于HTML5利用摄像头实现视频播放,截取摄像头图像,上传截取的图像。 需要注意的是,上传图像是基于PHP的,所以务必将这些文件架设在可以运行PHP的环境(如appserv)。
canvas与html5实现视频截图功能示例
这段时间一直在研究canvas,突发奇想想做一个可以截屏视频的功能,然后把图片拉去做表情包,哈哈哈哈哈哈~~ 制作方法: 1.在页面中加载视频 在使用canvas制作这个截图功能时,首先必须保证页面上已经加载完成了这个视频,这样才能够方便的对其操作。如果使用下面直接嵌入<video>标签的方式: <video loop controls id=testmp4 width=500 height=400 >
html5网页截图批注效果
通过html5canvas实现批注功能,可以在线批注,批注完成后保存成图片,或者通过网页截图的方式保存
html5 实现截图 代码实例
利用html5的api实现截图的示例,实现的效果当然没有qq那么好,不过也很值得学习


