• Canvas实现3D效果


    3D 球

    效果图

    请添加图片描述

    代码
    var canvas = document.getElementById("cas"),
        ctx = canvas.getContext("2d"),
        vpx = canvas.width / 2,
        vpy = canvas.height / 2,
        Radius = 150,
        balls = [],
        angleX = Math.PI / 1000,
        angleY = Math.PI / 1000,
    
        factor = 0.0001 //旋转因子
    
    
    var Animation = function () {
        this.init();
    };
    Animation.prototype = {
        init: function () {
            balls = [];
            var num = 500;
            for (var i = 0; i <= num; i++) {
                var k = -1 + (2 * (i + 1) - 1) / num;
                var a = Math.acos(k);
                var b = a * Math.sqrt(num * Math.PI);
                var x = Radius * Math.sin(a) * Math.cos(b);
                var y = Radius * Math.sin(a) * Math.sin(b);
                var z = Radius * Math.cos(a);
                var b = new ball(x, y, z, 1.5);
                balls.push(b);
                b.paint();
            }
        }
    }
    
    function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        rotateX();
        rotateY();
        balls.sort(function (a, b) {
            return b.z - a.z;
        })
        for (var i = 0; i < balls.length; i++) {
            balls[i].paint();
        }
    }
    
    function rotateX() {
        var cos = Math.cos(angleX);
        var sin = Math.sin(angleX);
        for (var i = 0; i < balls.length; i++) {
            var y1 = balls[i].y * cos - balls[i].z * sin;
            var z1 = balls[i].z * cos + balls[i].y * sin;
            balls[i].y = y1;
            balls[i].z = z1;
        }
    }
    
    function rotateY() {
        var cos = Math.cos(angleY);
        var sin = Math.sin(angleY);
        for (var i = 0; i < balls.length; i++) {
            var x1 = balls[i].x * cos - balls[i].z * sin;
            var z1 = balls[i].z * cos + balls[i].x * sin;
            balls[i].x = x1;
            balls[i].z = z1;
        }
    }
    
    var ball = function (x, y, z, r) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.r = r;
        this.width = 2 * r;
    }
    
    ball.prototype = {
        paint: function () {
            var fl = 450 //焦距
            ctx.save();
            ctx.beginPath();
            var scale = fl / (fl - this.z);
            var alpha = (this.z + Radius) / (2 * Radius);
            ctx.arc(vpx + this.x, vpy + this.y, this.r * scale, 0, 2 * Math.PI, true);
            ctx.fillStyle = "rgba(0,0,0," + (alpha + 0.5) + ")"
            ctx.fill();
            ctx.restore();
        }
    }
    
    var animation = new Animation();
    canvas.addEventListener('mousedown', onMousedown)
    
    function onMousedown() {
        window.addEventListener('mousemove', onMousemove)
        window.addEventListener('mouseup', onMouseup)
    }
    
    function onMousemove(e) {
        var x = e.clientX - canvas.offsetLeft - vpx - document.body.scrollLeft - document.documentElement
            .scrollLeft;
        var y = e.clientY - canvas.offsetTop - vpy - document.body.scrollTop - document.documentElement
            .scrollTop;
        angleY = -x * factor;
        angleX = -y * factor;
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        rotateX();
        rotateY();
        balls.sort(function (a, b) {
            return b.z - a.z;
        })
        for (var i = 0; i < balls.length; i++) {
            balls[i].paint();
        }
    }
    
    function onMouseup() {
        window.removeEventListener('mousemove', onMousemove)
        window.removeEventListener('mouseup', onMouseup)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    参考链接

    https://tool.4xseo.com/a/2285.html

  • 相关阅读:
    含文档+PPT+源码等]精品基于SSM的农产品交易平台[包运行成功]Java毕业设计SSM项目源码论文
    刷题记录(NC15665 maze,NC50243 小木棍)
    [正式学习java③]——字符串在内存中的存储方式、为什么字符串不可变、字符串的拼接原理,键盘录入的小细节。
    【设计模式】适配器模式:攻敌三分,自留七分,以超兽武装的例子来谈谈适配器模式
    c语言强制类型转换
    基于HTML+CSS+JavaScript制作响应式个人博客模板源码( JavaScript期末大作业 )
    Dubbo底层网络连接模型
    【博学谷学习记录】超强总结,用心分享|架构师-设计模式 1
    基于 SpringBoot + MyBatis 的博客系统
    电脑启动引导的两种方式
  • 原文地址:https://blog.csdn.net/qq_46258819/article/details/132581741