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

Suspension mode added #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 15 additions & 5 deletions src/BulletScreen.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { options, initBulletAnimate, isPlainObject, getContainer } from './helper';
import { defaultOptions, initBulletAnimate, isPlainObject, getContainer } from './helper';
import StyledBullet from './StyledBullet';

export default class BulletScreen {
target = null;
options = options;
options = defaultOptions;
bullets = [];
allPaused = false;
allHide = false;
Expand All @@ -14,7 +14,8 @@ export default class BulletScreen {
constructor(ele, opts = {}) {
// 更新默认配置项
this.options = Object.assign(this.options, opts);
const { trackHeight } = this.options;
console.log('construction ' + this.options.animate)
const { trackHeight, animate } = this.options;
// 设置弹幕目标
if (typeof ele === 'string') {
this.target = document.querySelector(ele);
Expand All @@ -36,7 +37,9 @@ export default class BulletScreen {
// this.target.style.overflow = 'hidden';
}
// 插入css animation
initBulletAnimate(this.target);
console.log(animate)
initBulletAnimate(this.target, 'RightToLeft');
initBulletAnimate(this.target, 'Suspension')
}
_getTrack() {
let readyIdxs = [];
Expand Down Expand Up @@ -67,9 +70,16 @@ export default class BulletScreen {
}

push(item, opts = {}) {
const options = Object.assign({}, this.options, opts);
console.log(this)
console.log('this ' + this.options.animate)
console.log('default ' + opts.animate)
let options = Object.assign({}, this.options, opts);

console.log({ options });

if (opts.animate) options.animate = opts.animate
console.log('push ' + options.animate)

const { onStart, onEnd, top } = options;
const bulletContainer = getContainer({
...options,
Expand Down
32 changes: 23 additions & 9 deletions src/helper.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import convert from 'color-convert';

const initBulletAnimate = screen => {
const initBulletAnimate = (screen, animateName='RightToLeft') => {
if (!screen) {
return;
}
const animateClass = 'BULLET_ANIMATE';
const animateClass = {
RightToLeft: 'BULLET_ANIMATE_RIGHT_TO_LEFT',
Suspension: 'BULLET_ANIMATE_SUSPENSION'
};

let style = document.createElement('style');
style.classList.add(animateClass);
style.classList.add(animateClass[animateName]);
document.head.appendChild(style);
// eslint-disable-next-line no-unused-vars
let { width } = screen.getBoundingClientRect();
let from = `from { visibility: visible; transform: translateX(${width}px); }`;
let to = `to { visibility: visible; transform: translateX(-100%); }`;
style.sheet.insertRule(`@keyframes RightToLeft { ${from} ${to} }`, 0);

const RightToLeftRule = `@keyframes RightToLeft {
from { visibility: visible; transform: translateX(${width}px); }
to { visibility: visible; transform: translateX(-100%); }
}`;
const SuspensionRule = `@keyframes Suspension {
from { visibility: visible; transform: translateX(calc(${width/2}px - 50%)); }
to { visibility: visible; transform: translateX(calc(${width/2}px - 50%)); }
}`;
const RuleSet = {
RightToLeft: RightToLeftRule,
Suspension: SuspensionRule
}
style.sheet.insertRule(RuleSet[animateName])
};
const isPlainObject = val => {
if (val === null) {
Expand Down Expand Up @@ -49,6 +62,7 @@ const getContainer = opts => {
// bulletContainer.style.zIndex = zIndex;
bulletContainer.style.visibility = 'hidden';
bulletContainer.style.animationName = animate;
console.log(animate)
bulletContainer.style.animationIterationCount = loopCount;
bulletContainer.style.animationDelay = isNaN(delay) ? delay : `${delay}s`;
bulletContainer.style.animationDirection = direction;
Expand Down Expand Up @@ -138,7 +152,7 @@ function getCorrectTextColor(rgb = [0, 0, 0]) {
}
}

const options = {
const defaultOptions = {
// 跑道高度
trackHeight: 50,
// 弹幕之间的间距
Expand All @@ -154,4 +168,4 @@ const options = {
direction: 'normal',
animateTimeFun: 'linear'
};
export { options, initBulletAnimate, isPlainObject, getContainer, getCorrectTextColor };
export { defaultOptions, initBulletAnimate, isPlainObject, getContainer, getCorrectTextColor };