0

0

js绘制抛物线代码分享

小云云

小云云

发布时间:2018-03-07 10:32:49

|

1823人浏览过

|

来源于php中文网

原创

本文主要和大家分享js绘制抛物线代码,我们先和大大家展示效果图,具体方法大家来一起看代码吧,希望能帮助到大家。

效果图:
这里写图片描述


    
    抛物线运动效果
    
    
    

回到JavaScript实现的抛物线运动效果

run

(function () {    var _$ = function (_this) {        return _this.constructor == jQuery ? _this : $(_this);
    };// 获取当前时间
    function now() {        return +new Date();
    }// 转化为整数
    function toInteger(text) {
        text = parseInt(text);        return isFinite(text) ? text : 0;
    }    var Parabola = function (options) {        this.initialize(options);
    };
    Parabola.prototype = {
        constructor: Parabola,        /**
         * 初始化
         * @classDescription 初始化
         * @param {Object} options 插件配置 .
         */
        initialize: function (options) {            this.options = this.options || this.getOptions(options);            var ops = this.options;            if (!this.options.el) {                return;
            }            this.$el = _$(ops.el);            this.timerId = null;            this.elOriginalLeft = toInteger(this.$el.css("left"));            this.elOriginalTop = toInteger(this.$el.css("top"));            // this.driftX X轴的偏移总量
            //this.driftY Y轴的偏移总量
            if (ops.targetEl) {                this.driftX = toInteger(_$(ops.targetEl).css("left")) - this.elOriginalLeft;                this.driftY = toInteger(_$(ops.targetEl).css("top")) - this.elOriginalTop;
            } else {                this.driftX = ops.offset[0];                this.driftY = ops.offset[1];
            }            this.duration = ops.duration;            // 处理公式常量
            this.curvature = ops.curvature;            // 根据两点坐标以及曲率确定运动曲线函数(也就是确定a, b的值)
            //a=this.curvature
            /* 公式: y = a*x*x + b*x + c;
             */
            /*
             * 因为经过(0, 0), 因此c = 0
             * 于是:
             * y = a * x*x + b*x;
             * y1 = a * x1*x1 + b*x1;
             * y2 = a * x2*x2 + b*x2;
             * 利用第二个坐标:
             * b = (y2+ a*x2*x2) / x2
             */
            // 于是
            this.b = ( this.driftY - this.curvature * this.driftX * this.driftX ) / this.driftX;            //自动开始
            if (ops.autostart) {                this.start();
            }
        },        /**
         * 初始化 配置参数 返回参数MAP
         * @param {Object} options 插件配置 .
         * @return {Object} 配置参数
         */
        getOptions: function (options) {            if (typeof options !== "object") {
                options = {};
            }
            options = $.extend({}, defaultSetting, _$(options.el).data(), (this.options || {}), options);            return options;
        },        /**
         * 定位
         * @param {Number} x x坐标 .
         * @param {Object} y y坐标 .
         * @return {Object} this
         */
        domove: function (x, y) {            this.$el.css({
                position: "absolute",
                left: this.elOriginalLeft + x,
                top: this.elOriginalTop + y
            });            return this;
        },        /**
         * 每一步执行
         * @param {Data} now 当前时间 .
         * @return {Object} this
         */
        step: function (now) {            var ops = this.options;            var x, y;            if (now > this.end) {                // 运行结束
                x = this.driftX;
                y = this.driftY;                this.domove(x, y);                this.stop();                if (typeof ops.callback === 'function') {
                    ops.callback.call(this);
                }
            } else {                //x 每一步的X轴的位置
                x = this.driftX * ((now - this.begin) / this.duration);                //每一步的Y轴的位置y = a*x*x + b*x + c;   c==0;
                y = this.curvature * x * x + this.b * x;                this.domove(x, y);                if (typeof ops.stepCallback === 'function') {
                    ops.stepCallback.call(this,x,y);
                }
            }            return this;
        },        /**
         * 设置options
         *  @param {Object} options 当前时间 .
         */
        setOptions: function (options) {            this.reset();            if (typeof options !== "object") {
                options = {};
            }            this.options = $.extend(this.options,options);            this.initialize(this.options);            return this;
        },        /**
         * 开始
         */
        start: function () {            var self = this;            // 设置起止时间
            this.begin = now();            this.end = this.begin + this.duration;            if (this.driftX === 0 && this.driftY === 0) {                // 原地踏步就别浪费性能了
                return;
            }            /*timers.push(this);
             Timer.start();*/
            if (!!this.timerId) {
                clearInterval(this.timerId);                this.stop();
            }            this.timerId = setInterval(function () {                var t = now();
                self.step(t);

            }, 13);            return this;
        },        /**
         * 重置
         */
        reset: function (x, y) {            this.stop();
            x = x ? x : 0;
            y = y ? y : 0;            this.domove(x, y);            return this;
        },        /**
         * 停止
         */
        stop: function () {            if (!!this.timerId) {
                clearInterval(this.timerId);

            }            return this;
        }
    };    var defaultSetting = {
        el: null,        //偏移位置
        offset: [0, 0],        //终点元素,这时就会自动获取该元素的left、top,设置了这个参数,offset将失效
        targetEl: null,        //运动的时间,默认500毫秒
        duration: 500,        //抛物线曲率,就是弯曲的程度,越接近于0越像直线,默认0.001
        curvature: 0.001,        //运动后执行的回调函数
        callback: null,        // 是否自动开始,默认为false
        autostart: false,        //运动过程中执行的回调函数
        stepCallback: null
    };
    window.Parabola = Parabola;
})();

相关推荐:

JS实现抛物线动画的代码实例

parabola.js实现抛物线与加入购物车效果

秘塔AI搜索
秘塔AI搜索

秘塔AI搜索,没有广告,直达结果

下载

两种JS实现小球抛物线轨迹运动的方法

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
React 教程
React 教程

共58课时 | 4万人学习

TypeScript 教程
TypeScript 教程

共19课时 | 2.4万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号