Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix canvas-ribbon error. #1932

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 67 additions & 50 deletions source/lib/canvas-ribbon/canvas-ribbon.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,77 @@
/**
* Created by zproo on 2017/4/3.
* Created by zproo on 2017/4/8.
*/
! function() {
document.addEventListener("touchstart", (e) => {
targetA = true;
!function () {
document.addEventListener('touchmove', function (e) {
e.preventDefault()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

});
document.addEventListener("touchmove", (e) => {
targetA = false
})
document.addEventListener("click", (e) => {
targetA = true
})
function e() {
if (targetA) {
for (a.clearRect(0, 0, d, r), i = [{
x: 0,
y: .7 * r + u
}, {
x: 0,
y: .7 * r - u
}]; i[1].x < d + u;) t(i[0], i[1])
targetA = false
}

function getAttr(script, attr, default_val) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier 'default_val' is not in camel case.

return Number(script.getAttribute(attr)) || default_val;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier 'default_val' is not in camel case.

}

// 获取自定义配置
var ribbon = document.getElementById('ribbon'); // 当前加载的script
config = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'config' is not defined.

zIndex: getAttr(ribbon, "zIndex", -1), // z-index

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.

alpha: getAttr(ribbon, "alpha", 0.6), // alpha

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.

ribbon_width: getAttr(ribbon, "size", 90), // size

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier 'ribbon_width' is not in camel case.
Mixed double and single quotes.

};

var canvas = document.createElement("canvas");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.

canvas.style.cssText = "position:fixed;top:0;left:0;z-index:"+config.zIndex;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.
'config' is not defined.

document.getElementsByTagName("body")[0].appendChild(canvas);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.


var canvasRibbon = canvas,
ctx = canvasRibbon.getContext('2d'), // 获取canvas 2d上下文
dpr = window.devicePixelRatio || 1, // the size of one CSS pixel to the size of one physical pixel.
width = window.innerWidth, // 返回窗口的文档显示区的宽高
height = window.innerHeight,
RIBBON_WIDTH = config.ribbon_width,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier 'ribbon_width' is not in camel case.
'config' is not defined.

path,
math = Math,
r = 0,
PI_2 = math.PI * 2, // 圆周率*2
cos = math.cos, // cos函数返回一个数值的余弦值(-1~1)
random = math.random; // 返回0-1随机数

canvasRibbon.width = width * dpr; // 返回实际宽高
canvasRibbon.height = height * dpr;
ctx.scale(dpr, dpr); // 水平、竖直方向缩放
ctx.globalAlpha = config.alpha; // 图形透明度

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'config' is not defined.


function init() {
ctx.clearRect(0, 0, width, height); // 擦除之前绘制内容
path = [{x: 0, y: height * 0.7 + RIBBON_WIDTH}, {x: 0, y: height * 0.7 - RIBBON_WIDTH}];
// 路径没有填满屏幕宽度时,绘制路径
while (path[1].x < width + RIBBON_WIDTH) {
draw(path[0], path[1])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

}
}

function t(e, t) {
a.beginPath(), a.moveTo(e.x, e.y), a.lineTo(t.x, t.y);
var o = t.x + (2 * x() - .25) * u,
c = n(t.y);
a.lineTo(o, c), a.closePath(), h -= m / -50, a.fillStyle = "#" + (127 * s(h) + 128 << 16 | 127 * s(h + m / 3) + 128 << 8 | 127 * s(h + m / 3 * 2) + 128).toString(16), a.fill(), i[0] = i[1], i[1] = {
x: o,
y: c
}
function draw(start, end) {
ctx.beginPath(); // 创建一个新的路径
ctx.moveTo(start.x, start.y); // path起点
ctx.lineTo(end.x, end.y); // path终点
var nextX = end.x + (random() * 2 - 0.25) * RIBBON_WIDTH,
nextY = geneY(end.y);
ctx.lineTo(nextX, nextY);
ctx.closePath();

r -= PI_2 / -50;
// 随机生成并设置canvas路径16进制颜色
ctx.fillStyle = '#' + (cos(r) * 127 + 128 << 16 | cos(r + PI_2 / 3) * 127 + 128 << 8 | cos(r + PI_2 / 3 * 2) * 127 + 128).toString(16);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.
Unexpected use of '<<'.
Unexpected use of '|'.

ctx.fill(); // 根据当前样式填充路径
path[0] = path[1]; // 起点更新为当前终点
path[1] = {x: nextX, y: nextY} // 更新终点

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

}

function n(e) {
var t = e + (2 * x() - 1.1) * u;
return t > r || 0 > t ? n(e) : t
function geneY(y) {
var temp = y + (random() * 2 - 1.1) * RIBBON_WIDTH;
return (temp > height || temp < 0) ? geneY(y) : temp;
}

var o = document.createElement("canvas");
o.style.cssText = "position:fixed;top:0;left:0;z-index:-1", document.getElementsByTagName("body")[0].appendChild(o);
var i, c = o,
a = c.getContext("2d"),
l = window.devicePixelRatio || 1,
d = window.innerWidth,
r = window.innerHeight,
u = 90,
f = Math,
h = 0,
m = 2 * f.PI,
s = f.cos,
x = f.random,
targetA = false;
c.width = d * l, c.height = r * l, a.scale(l, l), a.globalAlpha = .6, document.onclick = e, document.ontouchend = e, setTimeout(function() {
targetA = true;
e();
}, 100);
document.onclick = init;
document.ontouchstart = init;
init();
}();