从底部到顶部为圆柱体中的填充颜色设置动画

时间:2014-07-13 11:49:09

标签: jquery html5 css3 html5-canvas

当我在文本框中输入百分比值时,我已经使用画布(HTML)创建了画布圆柱体颜色正在填充圆柱体的底部到顶部。但我需要的是当它从底部移动到顶部时,它需要填充动画。

实际上我尝试过使用animate(Fade In),但我没有得到结果动画

<script src="jquery/jquery-1.11.1.min.js"></script>
        <script type="text/javascript">
            var perc = 0;
            $(document).ready(function () {
                $("#my_input").focusout(function (event) {
                    if ($("#my_input").val().indexOf("%") != -1) {

                        if ($.isNumeric($("#my_input").val().replace('%', ''))) {

                            // Allow only backspace and delete
                            if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 37) {
                                //$("#myCanvas").animate({ opacity: 0.25 });
                            } else {
                                // Ensure that it is a number and stop the keypress
                                if (event.keyCode < 48 || event.keyCode > 57) {
                                    event.preventDefault();
                                }
                            }
                            perc = parseInt($("#my_input").val().replace('%', '')) / 100;
                            draw();
                        }
                    } else {
                        alert('Value in %');
                    }
                });
            });
            function draw() {
                maxWidth = 180;
                maxHeight = 140;
                context.clearRect(0, 0, canvas.width, canvas.height);
                var x = 190;
                var y = 260;

                context.fillStyle = '#E2E2E2';
                context.beginPath();
                context.rect(x - maxWidth / 2, y - maxHeight, maxWidth, maxHeight);
                context.fill();

                var per = perc;
                if (per > 1) perc = 1;
                // fill
                context.fillStyle = 'yellow';

                context.beginPath();
                context.rect(x - maxWidth / 2, y - maxHeight * perc, maxWidth, maxHeight * perc);
                context.fill();
                context.animate({fill:'yellow'},200);
                drawCylinder(100, 100, 180, 180);
                context.beginPath();
            }
            </script>

    </head>
    <body>
    <canvas id="canvas" width="300px" height="300px" ></canvas>
    Numberic Value <input type ="text" id="my_input" class="numeric" name="my_input" placeholder="Enter value"/>        
            <script type='text/javascript'>//<![CDATA[ 
            //the below is code is for to work RequestAnimationFrame (SVG) in all the browsers -- Mahadevan         
            (function () {
                var lastTime = 0;
                var vendors = ['webkit', 'moz'];
                for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
                    window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
                    window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
                }

                if (!window.requestAnimationFrame) window.requestAnimationFrame = function (callback, element) {
                    var currTime = new Date().getTime();
                    var timeToCall = Math.max(0, 16 - (currTime - lastTime));
                    var id = window.setTimeout(function () {
                        callback(currTime + timeToCall);
                    },
                    timeToCall);
                    lastTime = currTime + timeToCall;
                    return id;
                };

                if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function (id) {
                    clearTimeout(id);
                };
            }());
            //the below code is to generate Cylinder object -- Mahadevan
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");

            var degreeAngle = 0;
            requestAnimationFrame(animate);

            function drawRotatedCylinder(x, y, w, h, degreeAngle) {
                context.save();
                context.translate(x + w / 10, y + h / 10);
                context.rotate(degreeAngle * Math.PI / 180);
                drawCylinder(-w / 10, -h / 10, w, h);
                context.restore();
            }

            function drawCylinder(x, y, w, h) {

                context.beginPath(); //to draw the top circle
                for (var i = 0 * Math.PI; i < 2 * Math.PI; i += 0.01) {

                    xPos = (x + w / 2) - (w / 2 * Math.sin(i)) * Math.sin(0 * Math.PI) + (w / 2 * Math.cos(i)) * Math.cos(0 * Math.PI);

                    yPos = (y + h / 8) + (h / 8 * Math.cos(i)) * Math.sin(0 * Math.PI) + (h / 8 * Math.sin(i)) * Math.cos(0 * Math.PI);

                    if (i == 0) {
                        context.moveTo(xPos, yPos);
                    } else {
                        context.lineTo(xPos, yPos);
                    }
                }
                context.moveTo(x, y + h / 8);
                context.lineTo(x, y + h - h / 8);

                for (var i = 0 * Math.PI; i < Math.PI; i += 0.01) {
                    xPos = (x + w / 2) - (w / 2 * Math.sin(i)) * Math.sin(0 * Math.PI) + (w / 2 * Math.cos(i)) * Math.cos(0 * Math.PI);
                    yPos = (y + h - h / 8) + (h / 8 * Math.cos(i)) * Math.sin(0 * Math.PI) + (h / 8 * Math.sin(i)) * Math.cos(0 * Math.PI);

                    if (i == 0) {
                        context.moveTo(xPos, yPos);

                    } else {
                        context.lineTo(xPos, yPos);
                    }
                }
                context.moveTo(x + w, y + h / 8);
                context.lineTo(x + w, y + h - h / 8);
                context.strokeStyle = '#ff0000';
                context.stroke();
                context.fillStyle = 'yellow';
                context.fill();
            }

            function animate() {
                requestAnimationFrame(animate);
                context.clearRect(0, 0, canvas.width, canvas.height);
                drawRotatedCylinder(100, 100, 180, 180, degreeAngle++);
                draw();
            }
            //]]> 
            </script>

我已从requestAnimateFrame(animate);

中删除了评论

我添加了

context.animate({'fill':yellow},300);

但仍然没有工作,请帮助我。

伙计们在上述问题上帮助我。

谢谢&amp;问候 马哈德

0 个答案:

没有答案