嗨,我發現很好的效果http://www.wandi-studio.com/Home/Welcome - 動畫文本bg。 (帶有標題的動畫背景HYT WATCHES,LE BATAFOR,MAURICE LACROIX)畫布文本與動畫bg
本網站使用mootools作爲js框架,我想用jQuery。
也許有人給我提示如何做出這個效果?
嗨,我發現很好的效果http://www.wandi-studio.com/Home/Welcome - 動畫文本bg。 (帶有標題的動畫背景HYT WATCHES,LE BATAFOR,MAURICE LACROIX)畫布文本與動畫bg
本網站使用mootools作爲js框架,我想用jQuery。
也許有人給我提示如何做出這個效果?
您可以使用畫布的context.globalCompositeOperation覆蓋文字與圖像。
然後,您可以通過對圖像的x偏移進行動畫處理來獲得所需的運動效果。
此代碼將在畫布上繪製文本,然後用圖像覆蓋只是文本。
// first draw the text on the canvas
ctx.beginPath();
ctx.font="144pt Verdana";
ctx.fillText("See",30,200);
ctx.fill();
// use compositing to draw the background image
// only where the text has been drawn
ctx.beginPath();
ctx.globalCompositeOperation="source-in";
ctx.drawImage(img,-x,0);
當然,你會想用你自己的文字字體,背景圖像和動畫模式風格化。
這裏是代碼和一個小提琴:http://jsfiddle.net/m1erickson/MrVJB/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ padding:20px; }
canvas{border:1px solid red; position:absolute}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var xOffset=100; // image offset
var fps=60;
var img=document.createElement("img");
img.onload=function(){
animate();
}
img.src="http://images4.fanpop.com/image/photos/23400000/water-water-23444632-2048-1277.jpg";
function draw(x){
ctx.save();
ctx.beginPath();
// put text on canvas
ctx.font="144pt Verdana";
ctx.fillText("See",30,200);
ctx.fill();
ctx.beginPath();
// use compositing to draw the background image
// only where the text has been drawn
ctx.globalCompositeOperation="source-in";
ctx.drawImage(img,-x,0);
ctx.restore();
}
function animate() {
// change the background image offset
xOffset+=3;
if(xOffset>=img.width){xOffset=0;}
// draw the text and background image
draw(xOffset);
// request another frame using fps intervals
setTimeout(function() {
requestAnimationFrame(animate);
// Drawing code goes here
}, 1000/fps);
}
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=400 height=300></canvas>
</body>
</html>
哇!做得好!這是一個非常全面的答案! – v2p