課程
/前端開發(fā)
/Html5
/Canvas玩兒轉(zhuǎn)紅包照片
誰知道 怎么做刮刮卡,涂抹的效果?思考題里 的最后一個。求代碼
2016-08-31
源自:Canvas玩兒轉(zhuǎn)紅包照片 3-5
正在回答
用圖片創(chuàng)建pattern 作為 strokeStyle 能相對簡單的實現(xiàn)
var?canvasWidth?=?2000; var?canvasHeight?=?1143; var?canvas?=?document.getElementById("canvas"); var?context?=?canvas.getContext("2d"); var?isMouseDown?=?false; var?imageReady?=?false; var?lastPoint?=?{x:?0,?y:?0}; canvas.width?=?canvasWidth; canvas.height?=?canvasHeight; var?pattern?; var?image?=?new?Image(); image.src?=?"image.jpg"; image.onload?=?function?()?{ ????imageReady?=?true; ????pattern?=?context.createPattern(image,"no-repeat"); }; function?windowToCanvas(x,?y)?{ ????var?bbox?=?canvas.getBoundingClientRect(); ????return?{ ????????x:?Math.round(x?-?bbox.left), ????????y:?Math.round(y?-?bbox.top), ????}; } canvas.onmousedown?=?function?(e)?{ ????e.preventDefault(); ????isMouseDown?=?true; ????lastPoint?=?windowToCanvas(e.clientX,?e.clientY); }; canvas.onmouseup?=?function?(e)?{ ????e.preventDefault(); ????isMouseDown?=?false; }; canvas.onmouseout?=?function?(e)?{ ????e.preventDefault(); ????isMouseDown?=?false; }; canvas.onmousemove?=?function?(e)?{ ????e.preventDefault(); ????if?(isMouseDown)?{ ????????var?curPoint?=?windowToCanvas(e.clientX,?e.clientY); ????????imageLine(curPoint,lastPoint); ????????lastPoint?=?curPoint; ????} }; function?imageLine(curPoint,lastPoint)?{ ????context.beginPath(); ????context.moveTo(curPoint.x,curPoint.y); ????context.lineTo(lastPoint.x,lastPoint.y); ????context.strokeStyle=pattern; ????context.lineWidth=20; ????context.lineCap="round"; ????context.stroke(); }
我這個效果實現(xiàn)了,思路不知道對不對,
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="UTF-8">
? ? <title>像素級圖像操作</title>
? ? <style>
#main {
? ? ? ? width: 1000px;
? ? ? ? margin: 0 auto;
? ? ? ? position: relative;
? ? ? ? overflow: hidden;
? ? ? ? height: 630px;
? ? ? ? border:2px solid #fff;
? ? ? ? -moz-user-select: none;
? ? ? ? -webkit-user-select: none;
? ? ? ? -ms-user-select: none;
? ? ? ? -khtml-user-select: none;
? ? ? ? user-select: none;
? ? }
? ? #image {
? ? ? ? display: block;
? ? ? ? width: 100%;
? ? ? ? position: absolute;
? ? ? ? z-index: 0;
? ? ? ? filter: blur(20px);
? ? ? ? -webkit-filter: blur(20px);
? ? ? ? -moz-filter: blur(20px);
? ? ? ? -ms-filter: blur(20px);
? ? ? ? -o-filter: blur(20px);
? ? #canvasA {
? ? #main a {
? ? ? ? width: 80px;
? ? ? ? height: 40px;
? ? ? ? text-align: center;
? ? ? ? line-height: 40px;
? ? ? ? border-radius: 5px;
? ? ? ? background: #eee;
? ? ? ? bottom: 20px;
? ? ? ? text-decoration: none;
? ? ? ? color: black;
? ? ? ? z-index: 2
? ? #main a:nth-of-type(1) {
? ? ? ? right: 20px;
}
? ? </style>
</head>
<body style="text-align:center;background:black">
? ? <div id="main">
? ? ? ? <img src="img.jpg" alt="" id="image">
? ? ? ? <canvas id="canvasA">
? ? ? ? ? ? 您的瀏覽器不支持該效果
? ? ? ? </canvas>
? ? ? ? <a href="javascript:show();">顯示</a>
? ? </div>
? ? <div></div>
? ? <script type="text/javascript">
? ? var canvasWidth = 1000;
? ? var canvasHeight = 630;
? ? // window.onload = function() {
? ? var canvasA = document.getElementById("canvasA");
? ? var ctxA = canvasA.getContext("2d");
? ? var onOff = false;
? ? canvasA.width = canvasWidth;
? ? canvasA.height = canvasHeight;
? ? var radius = 100;
? ? var image = new Image();
? ? image.src = "img.jpg";
? ? image.onload = function() {
? ? ? ? canvasA.addEventListener("mousemove", drawCircle, false)
? ? ? ? canvasA.addEventListener("mousedown", moveDown, false)
? ? ? ? canvasA.addEventListener("mouseup", moveUp, false)
? ? ? ? canvasA.addEventListener("mouseout", moveUp, false)
? ? function drawCircle(e) {
? ? ? ? var ev = ev || window.event;
? ? ? ? var x = ev.clientX - canvasA.getBoundingClientRect().left;
? ? ? ? var y = ev.clientY - canvasA.getBoundingClientRect().top;
? ? ? ? if (onOff) {
? ? ? ? ? ? if (radius < canvasHeight) {
? ? ? ? ? ? ? ? drawClip(x, y, radius)
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false
? ? function moveDown() {
? ? ? ? onOff = true;
? ? ? ? if (radius < canvasHeight) {
? ? ? ? ? ? drawCircle()
? ? ? ? ??
? ? function moveUp() {
? ? ? ? onOff = false;
? ? ? ? //console.log()
? ? ? ? ? ?// ctxA.clearRect(0, 0, canvasWidth, canvasHeight)
? ? function drawClip(x, y, r) {
? ? ? ? //ctxA.clearRect(0, 0, canvasWidth, canvasHeight)
? ? ? ? ctxA.save();
? ? ? ? ctxA.beginPath();
? ? ? ? ctxA.arc(x, y, r, 0, Math.PI * 2, false);
? ? ? ? //ctxA.closePath();
? ? ? ?
? ? ? ? ctxA.clip();
? ? ? ? ctxA.drawImage(image, 0, 0, canvasWidth, canvasHeight)
? ? ? ? ctxA.restore()
? ? ? ??
? ? function show() {
? ? ? ? var time = null;
? ? ? ? clearInterval(time)
? ? ? ? time = setInterval(function() {
? ? ? ? ? ? radius += 10;
? ? ? ? ? ? if (radius > canvasHeight) {
? ? ? ? ? ? ? ? clearInterval(time);
? ? ? ? ? ? ? ? //console.log("結(jié)束動畫",radius)
? ? ? ? ? ? drawClip(canvasWidth / 2, canvasHeight / 2, radius)
? ? ? ? }, 30)
? ? // }
? ? </script>
</body>
</html>
我的思路是借助
mousemove事件,把鼠標的經(jīng)過路徑(一個一個的x,y)放在一個數(shù)組里,這就是一條線....
在這條線上隨便取一些點畫圓,就出來手指涂抹效果了,不難。
代碼就沒興趣寫了。
fifter:blur()
舉報
教你玩兒轉(zhuǎn)微信紅包照片模糊效果,圖像展示效果,深入了解canvas
1 回答效果展示~~
2 回答點完show 之后立即點reset ,reset 的效果也是show的效果,大家遇到過沒????
5 回答點完show 之后立即點reset ,reset 的效果也是show的效果,大家遇到過沒????
1 回答裁剪沒有效果,求大神解答
2 回答怎么不共享demo出來
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2018-10-29
用圖片創(chuàng)建pattern 作為 strokeStyle 能相對簡單的實現(xiàn)
2018-05-29
我這個效果實現(xiàn)了,思路不知道對不對,
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="UTF-8">
? ? <title>像素級圖像操作</title>
? ? <style>
#main {
? ? ? ? width: 1000px;
? ? ? ? margin: 0 auto;
? ? ? ? position: relative;
? ? ? ? overflow: hidden;
? ? ? ? height: 630px;
? ? ? ? border:2px solid #fff;
? ? ? ? -moz-user-select: none;
? ? ? ? -webkit-user-select: none;
? ? ? ? -ms-user-select: none;
? ? ? ? -khtml-user-select: none;
? ? ? ? user-select: none;
? ? }
? ? #image {
? ? ? ? display: block;
? ? ? ? width: 100%;
? ? ? ? position: absolute;
? ? ? ? z-index: 0;
? ? ? ? filter: blur(20px);
? ? ? ? -webkit-filter: blur(20px);
? ? ? ? -moz-filter: blur(20px);
? ? ? ? -ms-filter: blur(20px);
? ? ? ? -o-filter: blur(20px);
? ? }
? ? #canvasA {
? ? ? ? display: block;
? ? ? ? position: absolute;
? ? }
? ? #main a {
? ? ? ? display: block;
? ? ? ? width: 80px;
? ? ? ? height: 40px;
? ? ? ? text-align: center;
? ? ? ? line-height: 40px;
? ? ? ? border-radius: 5px;
? ? ? ? background: #eee;
? ? ? ? position: absolute;
? ? ? ? bottom: 20px;
? ? ? ? text-decoration: none;
? ? ? ? color: black;
? ? ? ? z-index: 2
? ? }
? ? #main a:nth-of-type(1) {
? ? ? ? background: #eee;
? ? ? ? right: 20px;
? ? }
}
? ? </style>
</head>
<body style="text-align:center;background:black">
? ? <div id="main">
? ? ? ? <img src="img.jpg" alt="" id="image">
? ? ? ? <canvas id="canvasA">
? ? ? ? ? ? 您的瀏覽器不支持該效果
? ? ? ? </canvas>
? ? ? ? <a href="javascript:show();">顯示</a>
? ? </div>
? ? <div></div>
? ? <div></div>
? ? <div></div>
? ? <script type="text/javascript">
? ? var canvasWidth = 1000;
? ? var canvasHeight = 630;
? ? // window.onload = function() {
? ? var canvasA = document.getElementById("canvasA");
? ? var ctxA = canvasA.getContext("2d");
? ? var onOff = false;
? ? canvasA.width = canvasWidth;
? ? canvasA.height = canvasHeight;
? ? var radius = 100;
? ? var image = new Image();
? ? image.src = "img.jpg";
? ? image.onload = function() {
? ? ? ? canvasA.addEventListener("mousemove", drawCircle, false)
? ? ? ? canvasA.addEventListener("mousedown", moveDown, false)
? ? ? ? canvasA.addEventListener("mouseup", moveUp, false)
? ? ? ? canvasA.addEventListener("mouseout", moveUp, false)
? ? }
? ? function drawCircle(e) {
? ? ? ? var ev = ev || window.event;
? ? ? ? var x = ev.clientX - canvasA.getBoundingClientRect().left;
? ? ? ? var y = ev.clientY - canvasA.getBoundingClientRect().top;
? ? ? ? if (onOff) {
? ? ? ? ? ? if (radius < canvasHeight) {
? ? ? ? ? ? ? ? drawClip(x, y, radius)
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false
? ? }
? ? function moveDown() {
? ? ? ? onOff = true;
? ? ? ? if (radius < canvasHeight) {
? ? ? ? ? ? drawCircle()
? ? ? ? ??
? ? ? ? }
? ? }
? ? function moveUp() {
? ? ? ? onOff = false;
? ? ? ? //console.log()
? ? ? ? if (radius < canvasHeight) {
? ? ? ? ? ?// ctxA.clearRect(0, 0, canvasWidth, canvasHeight)
? ? ? ? }
? ? }
? ? function drawClip(x, y, r) {
? ? ? ? //ctxA.clearRect(0, 0, canvasWidth, canvasHeight)
? ? ? ? ctxA.save();
? ? ? ? ctxA.beginPath();
? ? ? ? ctxA.arc(x, y, r, 0, Math.PI * 2, false);
? ? ? ? //ctxA.closePath();
? ? ? ?
? ? ? ? ctxA.clip();
? ? ? ? ctxA.drawImage(image, 0, 0, canvasWidth, canvasHeight)
? ? ? ? ctxA.restore()
? ? ? ??
? ? }
? ? function show() {
? ? ? ? var time = null;
? ? ? ? clearInterval(time)
? ? ? ? time = setInterval(function() {
? ? ? ? ? ? radius += 10;
? ? ? ? ? ? if (radius > canvasHeight) {
? ? ? ? ? ? ? ? clearInterval(time);
? ? ? ? ? ? ? ? //console.log("結(jié)束動畫",radius)
? ? ? ? ? ? }
? ? ? ? ? ? drawClip(canvasWidth / 2, canvasHeight / 2, radius)
? ? ? ? }, 30)
? ? }
? ? // }
? ? </script>
</body>
</html>
2017-07-02
我的思路是借助
mousemove事件,把鼠標的經(jīng)過路徑(一個一個的x,y)放在一個數(shù)組里,這就是一條線....
在這條線上隨便取一些點畫圓,就出來手指涂抹效果了,不難。
代碼就沒興趣寫了。
2016-09-07
fifter:blur()