首页 > 其他分享 >【Canvas技法】绘制圆角六边形

【Canvas技法】绘制圆角六边形

时间:2024-09-27 13:51:18浏览次数:3  
标签:function Canvas ctx 圆角 HEIGHT WIDTH canvas 六边形 stage

【成图】

【Canvas技法】绘制圆角六边形_六边形

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>圆角六边形的制作Draft3展示</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body onl oad="init();">
        <div class="centerlize">
            <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">
                如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
            </canvas>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 高宽
const WIDTH=512;
const HEIGHT=512;

// 舞台对象
var stage;

//-------------------------------
// 初始化
//-------------------------------
function init(){
    // 获得canvas对象
    var canvas=document.getElementById('myCanvas');  
    canvas.width=WIDTH;
    canvas.height=HEIGHT;

    // 初始化canvas的绘图环境
    ctx=canvas.getContext('2d');  
    ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移

    // 准备
    stage=new Stage();    
    stage.init();

    // 开幕
    animate();
}

// 播放动画
function animate(){    
    stage.update();    
    stage.paintBg(ctx);
    stage.paintFg(ctx);     

    // 循环
    if(true){
        //sleep(100);
        window.requestAnimationFrame(animate);   
    }
}

// 舞台类
function Stage(){
    // 初始化
    this.init=function(){
        
    }

    // 更新
    this.update=function(){    
        
    }

    // 画背景
    this.paintBg=function(ctx){
        ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏    
    }

    // 画前景
    this.paintFg=function(ctx){
        // 底色
        ctx.save();
        ctx.fillStyle = "white";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        ctx.restore();

        // 调用函数绘制四个圆角六边形
        const N=4;
        const R=100*Math.sqrt(2);
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i+Math.PI/4;
            var a=createPt(R*Math.cos(theta),R*Math.sin(theta));
        
            ctx.save();
            ctx.lineWidth=2;
            ctx.fillStyle="RGB(215,204,182)";
            ctx.strokeStyle="RGB(104,20,20)";   
            drawRoundSixPolygon(ctx,a.x,a.y,80-i*5,10+i*5);
            ctx.stroke();
            ctx.fill();
            ctx.restore();
        }

        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*----------------------------------------------------------
函数:用于绘制圆角六边形
ctx:绘图上下文
x:六边形中心横坐标
y:六边形中心纵坐标
R:六边形半径
r:圆角半径
----------------------------------------------------------*/
function drawRoundSixPolygon(ctx,x,y,R,r){
    // 取点
    const N=6;// 六边形边数
    const ROUND=r;// 圆角半径
    
    var arr=[];// 放顶点三点的数组
    for(var i=0;i<N;i++){
        var theta=Math.PI*2/N*i;

        var r=R;
        var a=createPt(x+r*Math.cos(theta),y+r*Math.sin(theta));

        r=ROUND/Math.sqrt(3);
        var angle=theta+Math.PI/3*2;
        var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=ROUND/Math.sqrt(3);
        angle=theta-Math.PI/3*2;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        var arrInner=[b,a,c];
        arr.push(arrInner);
    }

    // 绘图
    ctx.save();
    ctx.beginPath();
    for(var i=0;i<arr.length;i++){
        var b=arr[i][0];
        var a=arr[i][1];
        var c=arr[i][2];

        ctx.lineTo(b.x,b.y);
        ctx.quadraticCurveTo(a.x,a.y,c.x,c.y);
        ctx.lineTo(c.x,c.y);
    }
    ctx.closePath();
    ctx.restore();
}


/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
color:填充圆的颜色
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,color){
    ctx.fillStyle=color;
    ctx.beginPath();
    ctx.arc(x,y,r,0,Math.PI*2,false);
    ctx.closePath();
    ctx.fill();
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
    var retval={};
    retval.x=x;
    retval.y=y;
    return retval;
}

/*----------------------------------------------------------
函数:延时若干毫秒
milliseconds:毫秒数
----------------------------------------------------------*/
function sleep(milliSeconds) {
    const date = Date.now();
    let currDate = null;
    while (currDate - date < milliSeconds) {
        currDate = Date.now();
    } 
}

/*----------------------------------------------------------
函数:书写文字
ctx:绘图上下文
x:横坐标
y:纵坐标
text:文字
font:字体
color:颜色
----------------------------------------------------------*/
function writeText(ctx,x,y,text,font,color){
    ctx.save();
    ctx.textBaseline="bottom";
    ctx.textAlign="center";
    ctx.font = font;
    ctx.fillStyle=color;
    ctx.fillText(text,x,y);
    ctx.restore();
}

/*-------------------------------------------------------------
愿意放弃自由来换取保障的人,最终既得不到自由,也得不到保障。
                                                      --哈耶克
--------------------------------------------------------------*/
//-->
</script>

END

标签:function,Canvas,ctx,圆角,HEIGHT,WIDTH,canvas,六边形,stage
From: https://blog.51cto.com/u_7726611/12128448

相关文章

  • 运用canvas绘制出好玩的粒子效果
    <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metaname="viewport"content="width=device-width,initial-scale=1.0">  <title>Document</title>  ......
  • WPF canvas Draw line , ellipse and rectangle, save canvas and contents as pictu
    //xaml<Windowx:Class="WpfApp417.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF InkCanvas selection mode, save all/selected strokes, load strokes file, sav
    //xaml<Windowx:Class="WpfApp416.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • 【Canvas与徽章】金穗蓝盾徽章
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>金穗蓝盾draft1</title><styletype="text/css&qu......
  • PyQt5 使用 QLabel 实现对图片圆角或者圆形图片
    PyQt5使用QLabel实现对图片圆角或者圆形图片本文圆角实现代码,是基于Qt处理图片:设置图片圆角样式,支持全圆角和部分圆角这篇文章将C++用Python重写得到,感谢!!实现方法就是使用QPainter与QPainterPath,将原QPixmap对象,先裁剪出一个圆角QPixmap对象并返回,最后通过QLabel的setPixmap......
  • 【Canvas与徽章】庆祝建国75周年徽章
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>庆祝建国75周年</title><styletype="text/css"......
  • 手写板 canvas
    <!doctypehtml><html><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge,chrome=1"><metaname="viewport"content="width=device-width">&......
  • Canvas简历编辑器-Monorepo+Rspack工程实践
    Canvas简历编辑器-Monorepo+Rspack工程实践 Canvas简历编辑器-Monorepo+Rspack工程实践在之前我们围绕Canvas聊了很多代码设计层面的东西,在这里我们聊一下工程实践。在之前的文中我也提到过,因为是本着学习的态度以及对技术的好奇心来做的,所以除了一些工具类的库例如 ArcoDe......
  • 【Canvas与诗词】铁马冰河入梦来
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>金红圈铁马冰河入梦来</title><styletype="text/css&quo......
  • 【Unity】UI、背景和3D的Camera和Canvas设置
    目前存在需求背景是指定的图片,该图片始终显示在页面中,不会因场景的视角操控发生尺寸等变化;UI内容显示在页面最上层,同样不会因场景的视角操控发生尺寸等变化,但是当软件整个尺寸发生变化时,会跟随变化,UI内容会覆盖3D物体;3D物体可以随着相机视角的变化而变近变远等,3D物体上可能存在......