Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
@font-face { font-family: 'Lato'; font-style: normal; font-weight: 900; src: url(https://fonts.gstatic.com/s/lato/v20/S6u9w4BMUTPHh50XSwiPHA.ttf) format('truetype'); } body { background-color: #24282f; margin: 0; padding: 0; } canvas { position: absolute; top: 0; left: 0; } #canvas-number { width: 680px; height: 420px; }
JavaScript
/* Desc: Define inital variables */ var numberStage, numberStageCtx, numberStageWidth = 680, numberStageHeight = 420, numberOffsetX, numberOffsetY, stage, stageCtx, stageWidth = window.innerWidth, stageHeight = window.innerHeight, stageCenterX = stageWidth/2, stageCenterY = stageHeight/2, countdownFrom = 10, countdownTimer = 2800, countdownRunning = true, number, dots = [], numberPixelCoordinates, circleRadius = 2, colors = ['61, 207, 236', '255, 244, 174', '255, 211, 218', '151, 211, 226']; /* Desc: Init canvases & Number text */ function init() { // Init stage which will have numbers numberStage = document.getElementById("canvas-number"); numberStageCtx = numberStage.getContext('2d'); // Set the canvas to width and height of the window numberStage.width = numberStageWidth; numberStage.height = numberStageHeight; // Init Stage which will have dots stage = document.getElementById("canvas-dots"); stageCtx = stage.getContext('2d'); stage.width = stageWidth; stage.height = stageHeight; // Create offset so text appears in middle of screen numberOffsetX = (stageWidth - numberStageWidth) / 2; numberOffsetY = (stageHeight - numberStageHeight) / 2; } init(); /* Desc: Dot object */ function Dot(x, y, color, alpha) { var _this = this; _this.x = x; _this.y = y; _this.color = color; _this.alpha = alpha; this.draw = function() { stageCtx.beginPath(); stageCtx.arc(_this.x, _this.y, circleRadius, 0, 2*Math.PI, false); stageCtx.fillStyle = 'rgba(' + _this.color + ', ' + _this.alpha + ')'; stageCtx.fill(); } } /* Desc: Create a certain amount of dots */ for (var i = 0; i < 2240; i++) { // Create a dot var dot = new Dot(randomNumber(0, stageWidth), randomNumber(0, stageHeight), colors[randomNumber(1, colors.length)], .3); // Push to into an array of dots dots.push(dot); // Animate dots tweenDots(dot, '', 'space'); } /* Desc: Countdown */ function countdown() { // Send number to be drawn drawNumber(countdownFrom.toString()); // When we hit zero stop countdown if (countdownFrom === 0) { countdownRunning = false; // Now that countdowns finised show the text Go drawNumber('GO'); } // Decrement number down countdownFrom--; } countdown(); /* Desc: Redraw loops */ function loop() { stageCtx.clearRect(0,0,stageWidth, stageHeight); for(var i = 0; i < dots.length; i++) { dots[i].draw(stageCtx); } requestAnimationFrame(loop); } loop(); /* Desc: Draw number */ function drawNumber(num) { // Create a number on a seperate canvas // Use a seperate canvas thats smaller so we have less data to loop over when using getImagedata() // Clear stage of previous numbers numberStageCtx.clearRect(0,0,numberStageWidth, numberStageHeight); numberStageCtx.fillStyle = "#24282f"; numberStageCtx.textAlign = 'center'; numberStageCtx.font = "bold 418px Lato"; numberStageCtx.fillText(num, 340, 400); var ctx = document.getElementById('canvas-number').getContext('2d'); // getImageData(x, y, width, height) // note: is an exspenisve function, so make sure canvas is small as possible for what you grab // Returns 1 Dimensional array of pixel color value chanels // Red, blue, green, alpha chanel of single pixel // First chanel is red var imageData = ctx.getImageData(0,0,numberStageWidth,numberStageHeight).data; // Clear number coordinated numberPixelCoordinates = []; // i is equal to total image data(eg: 480,000) // run while i is greater or equal to 0 // every time we run it minus 4 from i. Do this because each pixel has 4 chanels & we are only interested in individual pixels for (var i = imageData.length; i >= 0; i -= 4) { // If not an empty pixel if (imageData[i] !== 0) { // i represents the position in the array a red pixel was found // (i / 4 ) and percentage by width of canvas // Need to divide i by 4 because it has 4 values and you need its orginal position // Then you need to percentage it by the width(600) because each row contains 600 pixels and you need its relative position in that row var x = (i / 4) % numberStageWidth; // (i divide by width) then divide by 4 // Divide by width(600) first so you get the rows of pixels that make up the canvas. Then divide by 4 to get its postion within the row var y = Math.floor(Math.floor(i/numberStageWidth)/4); // If position exists and number is divisble by circle plus a pixel gap then add cordinates to array. So circles do not overlap if((x && x%(circleRadius * 2 + 3) == 0) && (y && y%(circleRadius * 2 + 3) == 0)) { // Push object to numberPixels array with x and y coordinates numberPixelCoordinates.push({x: x, y: y}); } } } formNumber(); } /* Desc: Form number */ function formNumber() { for (var i = 0; i < numberPixelCoordinates.length; i++) { // Loop out as many coordionates as we need & pass dots in to animate tweenDots(dots[i], numberPixelCoordinates[i], ''); } // Break number apart if (countdownRunning && countdownFrom > 0) { setTimeout(function() { breakNumber(); }, countdownTimer); } } function breakNumber() { for (var i = 0; i < numberPixelCoordinates.length; i++) { tweenDots(dots[i], '', 'space'); } if (countdownRunning) { // Build next number setTimeout(function() { countdown(); }, countdownTimer); } } /* Desc: Animate dots */ function tweenDots(dot, pos, type) { // Move dots around canvas randomly if (type === 'space') { // Tween dot to coordinate to form number TweenMax.to(dot, (3 + Math.round(Math.random() * 100) / 100), { x: randomNumber(0, stageWidth), y: randomNumber(0, stageHeight), alpha: 0.3, ease: Cubic.easeInOut, onComplete: function() { tweenDots(dot, '', 'space'); } }); } else { // Tween dot to coordinate to form number TweenMax.to(dot, (1.5 + Math.round(Math.random() * 100) / 100), { x: (pos.x + numberOffsetX), y: (pos.y + numberOffsetY), delay: 0, alpha: 1, ease: Cubic.easeInOut, onComplete: function() { } }); } } /* Desc: Get a random number */ function randomNumber(min, max) { return Math.floor(Math.random() * (max - min) + min); }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>canvas粒子点倒计时-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号