首页 > 其他分享 >【Canvas与艺术】十边螺线动态缩小光阑

【Canvas与艺术】十边螺线动态缩小光阑

时间:2024-09-02 21:52:55浏览次数:5  
标签:function Canvas ---------------------------------------------------------- ctx c

【成图】

【Canvas与艺术】十边螺线动态缩小光阑_canvas 光阑

【Canvas与艺术】十边螺线动态缩小光阑_canvas 光阑_02

【Canvas与艺术】十边螺线动态缩小光阑_canvas 光阑_03

【Canvas与艺术】十边螺线动态缩小光阑_canvas 光阑_04

【Canvas与艺术】十边螺线动态缩小光阑_canvas 光阑_05

【Canvas与艺术】十边螺线动态缩小光阑_canvas 光阑_06

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>十边螺线动态缩小光阑</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>
            <img id="myImg" src="342.jpg" style="display:none;"/>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

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

// 舞台对象
var stage;

var times=0;

//-------------------------------
// 初始化
//-------------------------------
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(times<=60){
        sleep(200);
        times++;
        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.fillStyle = "white";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        
        const R=180;// 初始半径,多边形顶点到屏幕中心的距离
        var colors=["rgb(253,1,0)","rgb(240,90,40)","rgb(244,172,62)",
                    "rgb(215,223,49)","rgb(58,181,71)","rgb(0,166,156)",
                    "rgb(39,168,223)","rgb(43,58,143)","rgb(101,45,144)",
                    "rgb(158,31,100)"];
        
        const DELTA=Math.PI/20;// 每次转动的角度
        const N=10;// 边数
        const ANGLE=(N-2)*Math.PI/2/N;
        const RATIO=Math.sin(ANGLE)/Math.sin(Math.PI-ANGLE-DELTA);// 根据正弦定理计算半径缩小比例    
        const TIMES=times;// 往里画的次数

        // 定义数组
        var arr=[];
        
        for(var i=0;i<N;i++){            
            var arrTmp=new Array();
            var r=R;

            for(var j=0;j<=TIMES;j++){
                r=r*RATIO;
                var theta=DELTA*j+ANGLE-Math.PI*2/N*i;
                var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));
                arrTmp.push(pt);
            }

            arr[i]=arrTmp;
        }

        for(var i=0;i<N;i++){
            var currArr=arr[i];
            var nextArr=arr[(i+1)%N]

            ctx.fillStyle=colors[i];
            //ctx.strokeStyle=colors[i];
            ctx.beginPath();
            for(var j=0;j<=TIMES;j++){
                var pt=currArr[j];                
                ctx.lineTo(pt.x,pt.y);
            }
            for(var j=TIMES;j>0;j--){
                var pt=nextArr[j];                
                ctx.lineTo(pt.x,pt.y);
            }
            ctx.closePath();
            ctx.fill();
        }
        

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

/*----------------------------------------------------------
函数:创建一个二维坐标点
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,c
From: https://blog.51cto.com/u_7726611/11900928

相关文章

  • 【Canvas与诗词】录王昌龄诗《从军行之五》红旗半卷出辕门...
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>已报生擒吐谷浑</title><styletype="text/css&......
  • vue使用html2canvas将页面dom生成图片,解决页面中带有图片导出
    安装npm installhtml2canvas引入importhtml2canvas from 'html2canvas'使用this.$refs.canvasToPic是div的dom,只要在这个div中的区域都可以生成图片1this.$nextTick(()=>{2html2canvas(this.$refs.canvasToPic,{useCORS:true,logging:true}).......
  • WPF mouse down on canvas and draw shapes which render with random colors
    //customcontrol//xaml<UserControlx:Class="WpfApp307.ElpTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"......
  • 【Canvas与艺术】夏日绝句 李清照
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>夏日绝句</title><styletype="text/css">......
  • 【Canvas与艺术】六边形漩涡
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>六边形漩涡</title><styletype="text/cs......
  • 图片作为Canvas贴图时要等图片加载完才可以读取canvas
    一、效果二、第一步:canvas.js中封装canvas函数,生成一个canvas对象,标注文字为参数nameunctioncreateCanvas(name){  /**   *创建一个canvas对象,绘制几何图案或添加文字   */  constcanvas=document.createElement("canvas");  constarr=......
  • canvas实现睡眠波2.0,框架兼容,支持大部分PC端
    npm引入方式npmi@JayVone/sleepWave//使用方式1import{sleepWindow}from'@JayVone/sleepWave'//获取dom然后绑定constapp=document.getElementById('app');sleepWindow.init(app);//在需要绘制的时候setOptionssleepWindow.setOption({types:['......
  • canvas 如何自动去换行
    在HTMLcanvas上绘制文本时,如果文本超出了canvas的宽度,它不会自动换行。要实现自动换行,你需要手动计算文本的长度并在适当的位置进行换行。以下是一个简单的JavaScript函数,它使用canvas的measureText方法来计算文本的长度,并在达到指定宽度时自动换行:functionwrapText(context,......
  • 【Canvas与艺术】十边曲线形光阑
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>十边曲线型光阑</title><styletype="text/css&q......
  • 【Canvas电脑桌面】蓝底金圈纹桌面(1920*1080)
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>蓝底金圈纹桌面</title><styletype="text/css&quo......