1.ç¨Androidå®ç°ç»å¤è¾¹å½¢ç®å¤´
2.怎么用html5的画箭canvas实现箭头随着鼠标移动和旋转
ç¨Androidå®ç°ç»å¤è¾¹å½¢ç®å¤´
æ¯è¾å®¹æï¼ ä½ä½ è¦çæ以ä¸å 容ï¼
1ã æ©å±æ åçViewï¼å®ç°å ¶onDrawæ¹æ³
public Paint paint=new Paint (Paint.ANTI_ALIAS_FLAG);
protected void onDraw(Canvas canvas) { ...}
2ãçæpaintåcanvasçç¨æ³
3. å¨onDrawæ¹æ³ä¸ç»å¤è¾¹å½¢ï¼Androidçå¤è¾¹å½¢æ¯ä»¥Pathè·¯å¾æ¥æè¿°çã
3ãçæPathè·¯å¾çç¨æ³:
以ä¸ä»£ç ç»ä¸ä¸ªç®å¤´
Path mPath=new Path();
mPath.moveTo(0, -);
mPath.lineTo(-, );
mPath.lineTo(0, );
mPath.lineTo(, );
mPath.close();
æå:å©ç¨canvasæpathç»åºæ¥ï¼ canvas.drawPath(mPath)
å¦å¤pathè¿æå ¶å®æ¹æ³å¯ä»¥å¢å ä¸ä¸ªç©å½¢æ弧形è¿å»ã
å¦æè¦å¡«å å°±æpaintçstyleæ¹æå¡«å å½¢ï¼ä¸ç¶å°±æ¯æ边形ã
pathæåä¸å¥ä¸å®è¦å°éï¼å³mPath.close();
ç¥å¦ä¹ æå¿«ã æ空å¯ççsdkä¸å ³äºPathç详ç»è¯´æã æåªæ¯åè¯ä½ ä¸ä¸ªå¤§æ¦ï¼å ·ä½ä½ éèªå·±ä½ä¼ã
怎么用html5的canvas实现箭头随着鼠标移动和旋转
下面是源码主文件
test.htm
<!doctype html>
<html>
<head>
<mata charset="utf-8">
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas" width="" height="">
<p> :( 抱歉~ <br> 您的浏览器貌似不支持HTML5的标签"canvas"的说,试试更换成
Chrome,头源FireFox,IE9...</p>
</canvas>
<script src="arrow.js"></script>
<script src="utils.js"></script>
<script>
window.onload=function(){
var canvas=document.getElementById("canvas"),
context=canvas.getContext('2d'),
mouse=utils.captureMouse(canvas),
arrow=new Arrow();
arrow.x=canvas.width/2;
arrow.y=canvas.height/2;
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = (window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
return window.setTimeout(callback, /);
});
}
(function drawFrame(){
window.requestAnimationFrame(drawFrame,canvas);
context.clearRect(0,0,canvas.width,canvas.height);
var dx=mouse.x-arrow.x;
var dy=mouse.y-arrow.y;
arrow.rotation=Math.atan2(dy,dx);
arrow.draw(context);
}());
};
</script>
</body>
</html>
var canvas=document.getElementById(“canvas”)
//即将变量 canvas 作为对 html5 canvas标签id为’canvas’ 的引用
context=canvas.getContext(‘2d’)
//获取canvas该对象后,可在其上进行图形绘制
window.requestAnimationFrame
为了便于JavaScript进行图形的码画USDT跑分源码开发重绘,各大浏览器厂商都提供了各自的画箭API给开发者进行调用,由于各大厂商的头源pyqt 源码 编译对HTML5的支持不同,所以API没有统一,码画c exlistview 源码但使用厂商各自的画箭API则在该API在对应浏览器上为最有效率的方式运行。代码中对
用户浏览器做判断,头源实例化能被成功引用的码画API接口。如果用户的画箭浏览器没有提供该API,则使用JS的setTimeout。其特性类似于AS的头源 ENTER_FRAME 事件。
需要用到的码画2个JS文件
utils.js 可根据传入的对象判断,鼠标所在对象的画箭麻将源码交易相对于左上角的坐标值
unction utils(){ };utils.captureMouse=function(element){
var mouse={ x:0,y:0};
element.addEventListener('mousemove',function(event){
var x,y;
if(event.pageX || event.pageY){
x=event.pageX;
y=event.pageY;
}else{
x=event.clientX+document.body.scrollLeft+
document.documentElement.scrollLeft;
y=event.clientY+document.body.scrollTop+
document.documentElement.scrollTop;
}
x -= element.offsetLeft;
y -= element.offsetTop;
mouse.x=x;
mouse.y=y;
},false);
return mouse;
};
计算mouse相对于容器的x,y坐标偏移,本质是头源判断鼠标在浏览器中的鼠标偏移,之后对浏览器中容器宽度和高度进行再次偏移。码画jni env源码
arrow.js
绘制一个箭头的js
function Arrow(){ this.x=0; this.y=0; this.color="#ffff"; this.rotation=0;}Arrow.prototype.draw=function(context){ context.save(); context.translate(this.x,this.y); context.rotate(this.rotation); context.lineWidth=2; context.fillStyle=this.color; context.beginPath(); context.moveTo(-,-); context.lineTo(0,-); context.lineTo(0,-); context.lineTo(,0); context.lineTo(0,); context.lineTo(0,); context.lineTo(-,); context.lineTo(-,-); context.closePath(); context.stroke(); context.restore(); };
熟悉AS的Graphics 的coder一定很快能熟悉使用JS的绘图API
style.css
用到的样式表
body{
background-color:#bbb;
}
#canvas{
background-color:#fff;
}
区分canvas 内外的颜色。