Hiệu ứng Canvas





Đầu tiên chèn CSS:
canvas { position: absolute; top: 0; left: 0; }

Tiếp đến là đoạn code dán vào nơi hiển thị:
<canvas id=c></canvas>
<script>
var w = c.width = window.innerWidth,
h = c.height = window.innerHeight,
ctx = c.getContext('2d'),
opts = {
len: 20,
count: 50,
baseTime: 10,
addedTime: 10,
dieChance: .05,
spawnChance: 1,
sparkChance: .1,
sparkDist: 10,
sparkSize: 2,
color: 'hsl(hue,100%,light%)',
baseLight: 50,
addedLight: 10,
shadowToTimePropMult: 6,
baseLightInputMultiplier: .01,
addedLightInputMultiplier: .02,
cx: w / 2,
cy: h / 2,
repaintAlpha: .04,
hueChange: .1
},
tick = 0,
lines = [],
dieX = w / 2 / opts.len,
dieY = h / 2 / opts.len,
baseRad = Math.PI * 2 / 6;
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, w, h);
function loop() {
window.requestAnimationFrame(loop); ++tick;
ctx.globalCompositeOperation = 'source-over';
ctx.shadowBlur = 0;
ctx.fillStyle = 'rgba(0,0,0,alp)'.replace('alp', opts.repaintAlpha);
ctx.fillRect(0, 0, w, h);
ctx.globalCompositeOperation = 'lighter';
if (lines.length < opts.count && Math.random() < opts.spawnChance) lines.push(new Line);
lines.map(function(line) {
line.step()
})
}
function Line() {
this.reset()
}
Line.prototype.reset = function() {
this.x = 0;
this.y = 0;
this.addedX = 0;
this.addedY = 0;
this.rad = 0;
this.lightInputMultiplier = opts.baseLightInputMultiplier + opts.addedLightInputMultiplier * Math.random();
this.color = opts.color.replace('hue', tick * opts.hueChange);
this.cumulativeTime = 0;
this.beginPhase()
}
Line.prototype.beginPhase = function() {
this.x += this.addedX;
this.y += this.addedY;
this.time = 0;
this.targetTime = (opts.baseTime + opts.addedTime * Math.random()) | 0;
this.rad += baseRad * (Math.random() < .5 ? 1 : -1);
this.addedX = Math.cos(this.rad);
this.addedY = Math.sin(this.rad);
if (Math.random() < opts.dieChance || this.x > dieX || this.x < -dieX || this.y > dieY || this.y < -dieY) this.reset()
}
Line.prototype.step = function() {++this.time; ++this.cumulativeTime;
if (this.time >= this.targetTime) this.beginPhase();
var prop = this.time / this.targetTime,
wave = Math.sin(prop * Math.PI / 2),
x = this.addedX * wave,
y = this.addedY * wave;
ctx.shadowBlur = prop * opts.shadowToTimePropMult;
ctx.fillStyle = ctx.shadowColor = this.color.replace('light', opts.baseLight + opts.addedLight * Math.sin(this.cumulativeTime * this.lightInputMultiplier));
ctx.fillRect(opts.cx + (this.x + x) * opts.len, opts.cy + (this.y + y) * opts.len, 2, 2);
if (Math.random() < opts.sparkChance) ctx.fillRect(opts.cx + (this.x + x) * opts.len + Math.random() * opts.sparkDist * (Math.random() < .5 ? 1 : -1) - opts.sparkSize / 2, opts.cy + (this.y + y) * opts.len + Math.random() * opts.sparkDist * (Math.random() < .5 ? 1 : -1) - opts.sparkSize / 2, opts.sparkSize, opts.sparkSize)
}
loop();
window.addEventListener('resize',
function() {
w = c.width = window.innerWidth;
h = c.height = window.innerHeight;
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, w, h);
opts.cx = w / 2;
opts.cy = h / 2;
dieX = w / 2 / opts.len;
dieY = h / 2 / opts.len
});

</script>
CÓ THỂ BẠN ĐANG TÌM
  • Code Nút Social share cho blogspot Blogger
    Code Nút Social share cho blogspot Blogger
    21/05/2018 - 0 bình luận
    Button chia sẻ bài viết lên các trang mạng xã hội là 1 thành phần không thể thiếu với mỗi template blogger. Việ…
  • Code Trang 404 Hiệu Ứng Đẹp
    Code Trang 404 Hiệu Ứng Đẹp
    04/08/2018 - 0 bình luận
    DemoCSS:@import url("https://fonts.googleapis.com/css?family=Lato|Russo+One");.container { position: absolute;…
  • CSS3 Hiệu ứng ẩn hiện cho blogspot
    CSS3 Hiệu ứng ẩn hiện cho blogspot
    05/05/2018 - 0 bình luận
    DemoChèn code dưới vào chỗ cần đặt:<div class="container"> <div class="accordion"> <dl> &l…
  • Template Blogspot Games APK Download 2018
    Template Blogspot Games APK Download 2018
    14/05/2018 - 0 bình luận
    Demo   Liên hệ#Template Blogspot #Template Blogspot Games  #Template Blogspot APPS
  • Hiệu ứng Hover cho img Chất cho blogspot
    Hiệu ứng Hover cho img Chất cho blogspot
    14/05/2018 - 0 bình luận
    DemoĐầu tiên bạn kiểm tra blog bạn có link css này chưa, chưa có thì chèn thêm vào link css dưới: <link href…
  • CSS3 Hiệu ứng button đẹp
    CSS3 Hiệu ứng button đẹp
    07/05/2018 - 0 bình luận
    DemoĐầu tiên chèn css dưới vào:.centerer { width: 100%; max-width: 600px; margin: 0 auto;}.wrap { width: 50…
  • Thẻ điều kiện blogger 2018
    Thẻ điều kiện blogger 2018
    18/05/2018 - 0 bình luận
    Dưới đây là danh sách thẻ cũ và thẻ mới được cập nhật và cách sử dụngPhần I. Các thẻ điều kiện1. Trang chủ (Hom…
  • Tạo dropdown label (nhãn) cho blogspot
    Tạo dropdown label (nhãn) cho blogspot
    22/05/2018 - 0 bình luận
    Nhãn (Label, Tags, Categories...) là một thành phần rất quan trọng khi bạn sử dụng nền tảng blogger. Nhãn giúp …
  • Code Hiệu Ứng Hộp Search Tìm Kiếm Đẹp Cho Blogspot
    Code Hiệu Ứng Hộp Search Tìm Kiếm Đẹp Cho Blogspot
    15/05/2018 - 0 bình luận
    DemoĐầu tiền Chèn đoạn code hiển thị khung search dưới đây: <label class="input-container closed"> &…
  • Tổng hợp 8 CSS Hiệu ứng Loading
    Tổng hợp 8 CSS Hiệu ứng Loading
    30/06/2018 - 0 bình luận
    DemoĐầu tiên chèn đoạn mã dưới vào nơi hiển thị hiệu ứng loading:<div class="cell"> <div class="wrapp…
  • CSS Hiệu Ứng Thầy Trò Tây Du Ký Thỉnh Kinh
    CSS Hiệu Ứng Thầy Trò Tây Du Ký Thỉnh Kinh
    08/07/2018 - 0 bình luận
    DemoChèn CSS dưới vào:* { padding: 0; margin: 0; list-style: none; } html,body { height: 100%; } .main { h…
  • Menu ẩn hiện dạng Material Design cho blogspot
    Menu ẩn hiện dạng Material Design cho blogspot
    21/05/2018 - 0 bình luận
    Menu là thành phần không thể thiếu đối với mỗi website, 1 thanh menu chưa đủ với bạn ? bạn cần 1 thanh menu ẩn …