//get random number in certain range
function RandomNum(Min,Max) {
var Range = Max - Min;
var Rand = Math.random();
var num = Min + Math.round(Rand * Range);
return num;
}
//time number add 0 before if <10
function plusZero(x) {
if (x < 10) {
x = "0" + x;
} else {
x = x;
}
return x;
}
//start danmu
function startDanmu() {
var message = $("input");
var messageVal = message.val();
var danmuMessage = "" + messageVal + "";
//get random color HEX
//u can also save the colors u want by array
var color = RandomNum(100000,999999);
//get random danmu speed
var speed = RandomNum(10000,20000);
//get random position Y
//danmu box height is 450,we set the danmu position Y max 400 in case it blocks the subtitle
var positionY = RandomNum(50,400);
if (messageVal.length > 0) {
//insert danmu message into danmu box
$(".danmu-box").prepend(danmuMessage);
//have to use first() cuz we prepend the message,u can try what's gonna happen if no first()
//set it's style
$(".danmu-message").first().css({
"right": "0","top": positionY,"color": "#" + color
});
//set it's animation
//from right 0 to left 0
//hide it after move
$(".danmu-message").first().animate({
left: '0px',},speed,function() {
$(this).fadeOut();
});
//get danmu time
var time = new Date();
var month = time.getMonth() + 1;
var day = time.getDay();
var hour = time.getHours();
var minute = time.getMinutes();
var danmuTime = plusZero(month) + "-" + plusZero(day) + " " + plusZero(hour) + ":" + plusZero(minute);
//insert danmu message to table
if (messageVal.length > 6) {
messageVal = messageVal.substring(0,6) + "...";
}
var messageToTable = "<tr><td>" + messageVal + "</td><td>" + danmuTime + "</td></tr>";
$(".danmu-table > tbody").prepend(messageToTable);
} else {}
//empty the input
message.val("");
}
//clear danmu box
function clearDanmu() {
$(".danmu-box").html("");
}