Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
Your browser does not support the HTML5 Canvas element.
css
* { cursor: none; } html, body { background-color: #000000; overflow: hidden; padding: 0px; margin: 0px; } canvas { border: black solid 1px; }
JavaScript
$(function() { // Define variables var canvas, width, height, context, diameter, radius, centreX, centreY; var white = '#FFFFFF'; var grey = '#424242'; var red = '#FF0000'; window.onresize = function(event) { init(); } function init() { canvas = $("#canvas")[0]; canvas.width = window.innerWidth canvas.height = window.innerHeight; width = canvas.width; height = canvas.height; //console.log("width: " + width); //console.log("height: " + height); centreX = Math.floor(width/2) + 0.5; centreY = Math.floor(height/2) + 0.5; //console.log("centreX: " + centreX); //console.log("centreY: " + centreY); context = canvas.getContext("2d"); clearCanvas(); drawDial(); } function clearCanvas() { context.fillRect(0, 0, width, height); } function drawDial() { diameter = Math.floor(Math.min(canvas.width, canvas.height) * 0.7); radius = Math.floor(diameter/2); diameter = radius * 2; //console.log("diameter: " + diameter); //console.log("radius: " + radius); // Draw cross hairs context.beginPath(); context.strokeStyle = grey; context.moveTo(centreX - radius, centreY); context.lineTo(centreX + radius, centreY); context.moveTo(centreX, centreY - radius); context.lineTo(centreX, centreY + radius); context.moveTo(centreX, centreY); var endObj = getPolarPoint(centreX, centreY, 45, radius); context.lineTo(endObj.x, endObj.y); context.stroke(); // Draw part circle context.beginPath(); context.strokeStyle = grey; var innerCircleRadius = radius*0.33; context.arc(centreX, centreY, radius*0.33, 0, degToRad(320), false); context.stroke(); // Draw arrow head var rotationOffsetDegrees = 10; var arrowHeadLegth = radius*0.05; var arrowHeadPoint = getPolarPoint(centreX, centreY, 320, innerCircleRadius); var endPoint1 = getPolarPoint(arrowHeadPoint.x, arrowHeadPoint.y, 270 + rotationOffsetDegrees, arrowHeadLegth); var endPoint2 = getPolarPoint(arrowHeadPoint.x, arrowHeadPoint.y, 180 + rotationOffsetDegrees, arrowHeadLegth); context.strokeStyle = grey; context.beginPath(); context.moveTo(arrowHeadPoint.x, arrowHeadPoint.y); context.lineTo(endPoint1.x, endPoint1.y); context.moveTo(arrowHeadPoint.x, arrowHeadPoint.y); context.lineTo(endPoint2.x, endPoint2.y); context.stroke(); // Draw outer circle context.beginPath(); context.arc(centreX, centreY, radius, 0 , 2 * Math.PI, false); context.lineWidth = 1; context.strokeStyle = white; context.stroke(); // Draw long outer dividing lines (Radians) context.beginPath(); var numLines = 40; var angleIncrement = 360/numLines; var lineLength = radius * 0.1; var startAngle = 0; for (var i=0; i
radius) { lineLength = radius; } var endPoint = getPolarPoint(centreX, centreY, angle, lineLength); context.strokeStyle = red; context.beginPath(); context.moveTo(centreX, centreY); context.lineTo(endPoint.x, endPoint.y); // Red arrow head context.moveTo(endPoint.x, endPoint.y); var redArrowHeadEnd1 = getPolarPoint(endPoint.x, endPoint.y, 220 + angle, 6); context.lineTo(redArrowHeadEnd1.x, redArrowHeadEnd1.y); context.moveTo(endPoint.x, endPoint.y); var redArrowHeadEnd2 = getPolarPoint(endPoint.x, endPoint.y, 140 + angle, 6); context.lineTo(redArrowHeadEnd2.x, redArrowHeadEnd2.y); context.stroke(); // Show red text var radians = degToRad(angle); var radiansStr = Math.floor(radians * 1000) / 1000; var degreesText = angleStr + "° = " + radiansStr + " radians"; context.font = (diameter*0.02) + 'px sans-serif'; if (angle < 180) { textPosObj = getPolarPoint(centreX, centreY, 270, (radius*0.1)); } else { textPosObj = getPolarPoint(centreX, centreY, 90, (radius*0.1)); } metrics = context.measureText(degreesText); var degreesTextWidth = metrics.width; context.fillStyle = red; context.fillText(degreesText, textPosObj.x - (degreesTextWidth/2), textPosObj.y + ((diameter*0.02)/3)); } function getPolarPoint($x, $y, $degrees, $distance) { var destinationPt = new Object(); destinationPt.x = $x + Math.round($distance * Math.cos( $degrees * Math.PI / 180 )); destinationPt.y = $y + Math.round($distance * Math.sin( $degrees * Math.PI / 180 )); return destinationPt; } function angleOfLine($x1, $y1, $x2, $y2){ return atan2Degrees($y2 - $y1, $x2 - $x1); } function distanceBetween2Points($x1, $y1, $x2, $y2) { var dx = $x2 - $x1; var dy = $y2 - $y1; return Math.sqrt( Math.pow( dx, 2 ) + Math.pow( dy, 2 ) ); } function atan2Degrees($y, $x){ return Math.atan2($y, $x) * (180/Math.PI); } function degToRad($degrees) { return $degrees * (Math.PI/180); } function radToDeg($radians) { return $radians * (180/Math.PI); } // Tracking mouse position in canvas // http://stackoverflow.com/questions/5085689/tracking-mouse-position-in-canvas function findPos($obj) { var curleft = 0, curtop = 0; if ($obj.offsetParent) { do { curleft += $obj.offsetLeft; curtop += $obj.offsetTop; } while ($obj = $obj.offsetParent); return { x: curleft, y: curtop }; } return undefined; } $('#canvas').mousemove(function(e) { var pos = findPos(this); var x = e.pageX - pos.x; var y = e.pageY - pos.y; init(); drawCursorPoint(x, y); }); init(); });
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>在画布上绘制的PI度数-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号