HTML5 Canvas学习笔记(2)菜单高亮显示与像素字体

看到哪,学到哪,记到哪。见谅,这些笔记就没有顺序和知识上的连贯性了。希望各位老师指出我学习中的错误!!
效果图:
[img]http://dl2.iteye.com/upload/attachment/0098/4033/f6d4c7a8-3799-31c3-b2da-4c61a5e291d1.gif[/img]

点击看效果:
[url]http://www.108js.com/article/canvas/2/menu.html[/url]

欢迎访问博主网站:[url]http://www.108js.com[/url]

代码:
<!doctype html>
<html>
<head>
<title>菜单高亮显示</title>
<style>
@font-face {
font-family: 'PixelFont';
src: url('font/pixelfont.eot') format('embedded-opentype'),
url('font/pixelfont.woff') format('woff'),
url('font/pixelfont.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
h1{
font: 22pt 'PixelFont';
}
</style>

</head>
<body style="background-color:#eee;" >
<h1>Menu</h1>
<div id="GameDiv" style="margin:0 auto;" >
<canvas id="GameCanvas" >
Your browser does not support the HTML5 canvas element.
</canvas>
</div>
<div id="hidden" style="visibility:hidden; width:1px; height:1px; overflow:hidden">
<img id="backgroundMenu" src="img/backgroundmenu.png" οnlοad="MainMenu(this);">
<img id="sprLogo" src="img/logo.png" οnlοad="MainMenu(this);">
<img id="sprSplashLogo" src="img/splashLogo.png" οnlοad="MainMenu(this);">
<img id="sprHTML" src="img/htmlit.png" οnlοad="MainMenu(this);">


</div>
</body>
</html>
<script>
var div = document.getElementById("GameDiv");
div.style.width = "768px";
div.style.height = "512px";
var canvas = document.getElementById("GameCanvas");
canvas.setAttribute("width","768");
canvas.setAttribute("height","512");

var ctx = canvas.getContext("2d");
var patternMenu =null;

var cx = canvas.width/2;
var cy = canvas.height/2;
var mouseX=10;
var mouseY=10;
var itemsLoaded=0;//当前加载完的图片数

function MainMenu(item){
itemsLoaded++;//当前加载完的图片数
if(itemsLoaded==4) {
ctx.save();
patternMenu=ctx.createPattern(backgroundMenu,"repeat");
ctx.fillStyle = patternMenu;
ctx.fillRect(0,0,canvas.width, canvas.height);
ctx.restore();
ctx.drawImage(sprLogo, canvas.width/2 - sprLogo.width/2 , 80);
ctx.drawImage(sprSplashLogo, 70 , 180);
ctx.textAlign = "start";
ctx.font = "12pt 'Segoe UI Light',Verdana";
ctx.fillStyle = "#eee";
ctx.fillText("HTML.it | Guida Videogame HTML5 di Adriano Tumminelli", 60,canvas.height-20);
ctx.drawImage(sprHTML, 5, canvas.height-55);
ctx.shadowOffsetX = 0;
ctx.shadowBlur = 0;
drawText();
}
}

function MouseInsideText(str, x, y, col1, col2){
ctx.shadowColor = "#000";
ctx.shadowOffsetX = 1;
ctx.font = "32pt 'PixelFont'"
ctx.textAlign = "center";
ctx.shadowBlur = 3;

var w = ctx.measureText(str).width;//字符串的宽
var h = 30;
var inside = (mouseX > x - w/2 && mouseY > y - h && mouseX < x + w/2 && mouseY < y+4 );
if(inside)//鼠标在文本上时
ctx.fillStyle = col2;
else
ctx.fillStyle = col1;
//ctx.textBaseline = 'middle';
//ctx.textAlign = 'center';
ctx.fillText(str, x, y);
return inside;
}

function drawText(){
MouseInsideText("New Game",cx, cy+10,"#eee", "#ea4");
MouseInsideText("Other games",cx, cy+80,"#eee", "#ea4");
setTimeout(drawText,100);
}

//鼠标移动
canvas.οnmοusemοve=function(e) {
var e = window.event || e
var rect = this.getBoundingClientRect();
mouseX =e.clientX - rect.left;//获取鼠标在canvsa中的坐标
mouseY =e.clientY - rect.top;

}

</script>

源码下载:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值