Home Web Front-end JS Tutorial js drawing parabola code sharing

js drawing parabola code sharing

Mar 07, 2018 am 10:32 AM
javascript share parabola

This article mainly shares with you the js drawing parabola code. Let's show you the renderings first. Let's take a look at the code together with the specific methods. I hope it can help you.

Rendering:
js drawing parabola code sharing

<!DOCTYPE HTML><html><head>
    <meta charset="utf-8">
    <title>抛物线运动效果</title>
    <style type="text/css">
        body {            overflow: hidden;        }
        .boll {            width: 50px;            height: 50px;            position: absolute;            top: 55%;            left: 55%;            background: url("redpack_show.jpg");            background-size: contain;        }
        .red-bag {            position: absolute;            left: 50%;            top:50%;            width: 200px;            height: 200px;            border-radius: 50%;            background: url("red_bag.png") no-repeat;            background-size: contain;            z-index:10;        }
    </style>
    <script type="text/javascript" src="jquery-2.1.4.js"></script>
    <script type="text/javascript" src="parabola.js"></script></head><body><p class="btns" style="margin-top:20px">
    <a href="http://www.css88.com/archives/5355" target="_blank">回到JavaScript实现的抛物线运动效果</a></p><p class="btns" style="margin-top:20px">
    <a href="#" class="btnA btn-danger" id="run" rel="popover" title="A Title" >run</a></p><p class="red-bag"></p><p id="target" class="target"></p><!--<p class="boll boll1"></p>--><script type="text/javascript">
    for(let i=1;i<80;i++){
        $("body").append(`<p class=&#39;boll boll${i}&#39;></p>`);
        let bool = "bool"+i;
        createSingle(bool,i);
//        console.log(i)
    }
    function createSingle(bool,i) {
        let left = i%2 == 0 ? (-getRandom(500,600)):(getRandom(500,600));
        let top = i%2 == 0? (getRandom(200,250)):(getRandom(200,250));
//        console.log(getRandom(0.005,0.01));
        bool = new Parabola({
            el: ".boll"+i,
            offset: [left, top],
            curvature: 0.005,
            duration: 3000,
            callback:function(){
//                alert("完成后回调")
            },
            stepCallback:function(x,y){
//                console.log(x,y);
//                $("<p>").appendTo("body").css({
//                    "position": "absolute",
//                    "top": this.elOriginalTop + y,
//                    "left":this.elOriginalLeft + x,
//                    "background-color":"rgba(244,21,50,.5)",
//                    "width":"2px",
//                    "height":"2px",
//                    "border-radius": "2px"
//                });
            }
        });
        $("#run").click(function (event) {
            event.preventDefault();
            bool.start();
        });
    }
    function getRandom(min,max) {
        return Math.random()*(max-min)+min;
    }

    //    $("#reset").click(function (event) {
    //        event.preventDefault();
    //        bool.reset()
    //    });
    //    $("#run").click(function (event) {
    //        event.preventDefault();
    //        bool.start();
    //    });
    //    $("#stop").click(function (event) {
    //        event.preventDefault();
    //        bool.stop();
    //    });
    //    $("#setOptions").click(function (event) {
    //        event.preventDefault();
    //        bool.setOptions({
    //            targetEl: $("#target"),
    //            curvature: 0.001,
    //            duration: 1000
    //        });
    //    });</script></body></html>
Copy after login
(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 === &#39;function&#39;) {
                    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 === &#39;function&#39;) {
                    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;
})();
Copy after login

Related recommendations:

JS code example to implement parabolic animation

parabola.js implements parabola and add to shopping cart effects

Two JS methods to achieve parabola trajectory movement of small balls

The above is the detailed content of js drawing parabola code sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to share Quark Netdisk to Baidu Netdisk? How to share Quark Netdisk to Baidu Netdisk? Mar 14, 2024 pm 04:40 PM

Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments Mar 25, 2024 am 11:41 AM

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the &quot;Share to&quot; option at the bottom, and then select the first &quot;WeChat Moments&quot; allows you to share content to WeChat Moments.

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to share files with friends on Baidu Netdisk How to share files with friends on Baidu Netdisk Mar 25, 2024 pm 06:52 PM

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the &quot;Shared Members&quot; column 】, and finally check all

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Mango tv member account sharing 2023 Mango tv member account sharing 2023 Feb 07, 2024 pm 02:27 PM

Mango TV has various types of movies, TV series, variety shows and other resources, and users can freely choose to watch them. Mango TV members can not only watch all VIP dramas, but also set the highest definition picture quality to help users watch dramas happily. Below, the editor will bring you some free Mango TV membership accounts for users to use, hurry up and take a look Take a look. Mango TV latest member account free sharing 2023: Note: These are the latest member accounts collected, you can log in directly and use them, do not change the password at will. Account number: 13842025699 Password: qds373 Account number: 15804882888 Password: evr6982 Account number: 13330925667 Password: jgqae Account number: 1703

Share two installation methods for HP printer drivers Share two installation methods for HP printer drivers Mar 13, 2024 pm 05:16 PM

HP printers are essential printing equipment in many offices. Installing the printer driver on the computer can perfectly solve problems such as the printer being unable to connect. So how to install HP printer driver? The editor below will introduce you to two HP printer driver installation methods. The first method: download the driver from the official website 1. Search the HP China official website in the search engine, and in the support column, select [Software and Drivers]. 2. Select the [Printer] category, enter your printer model in the search box, and click [Submit] to find your printer driver. 3. Select the corresponding printer according to your computer system. For win10, select the driver for win10 system. 4. After downloading successfully, find it in the folder

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles