[英]How to Plot circle graph using math Random?
I'm using javascript to make an sparkle like animation with css.我正在使用 javascript 来制作像 animation 和 css 这样的闪光。
<div class="r1">
<img src="star.png">
</div>
Each Dot每个点
.dotter{
width: 4px;
height: 4px;
background: blue;
border-radius: 50px;
position: absolute;
bottom: 50%;
left: 50%;
transform: translate(0,0);
}
Javascript Javascript
const block = document.querySelector('.r1');
const css = document.createElement('style');
function creator(){
var newKey = new String;
for(i=0;i<=100;i++){
var dot = document.createElement('div');
dot.classList.add('dotter');
dot.style.animation = `fadeOut${i} 1s linear infinite`;
var keyframes = `@keyframes fadeOut${i} {from {transform: translate(0,0) scale(1);opacity: 1;}to {transform: translate(${newRandom()[0]}%,${newRandom()[1]}%) scale(0.1);opacity: 0;}}`;
block.appendChild(dot);
newKey += keyframes;
}
css.innerText = newKey;
document.getElementsByTagName('body')[0].appendChild(css);
};
creator();
function newRandom(){
const n1p1 = Math.random()*900;
const n1p2 = Math.random()*(-900);
const n2p1 = Math.random()*(-900);
const n2p2 = Math.random()*900;
const n1 = Math.random() < 0.5 ? n1p1 : n1p2;
const n2 = Math.random() < 0.5 ? n2p1 : n2p2;
const m1 = Math.abs(n1);
const m2 = Math.abs(n2);
if( m1>=850 || m2>850 ){
if((m1 + m2) >= 900){
return [n2,n1];
}else{
return newRandom();
}
}else{
return newRandom();
}
};
Now the problem is when I run the code, the sparkles end-up forming a square outside the image, I need a numeric solution for how to make the sparkles end-up shaping a circle.现在的问题是,当我运行代码时,火花最终会在图像外形成一个正方形,我需要一个数值解决方案来解决如何使火花最终形成一个圆形。
I changed yout NewRandom()
function to achieve this, if helps我改变了
NewRandom()
function 来实现这个,如果有帮助
const block = document.querySelector('.r1'); const css = document.createElement('style'); function creator(){ var newKey = new String; for(i=0;i<=100;i++){ var dot = document.createElement('div'); dot.classList.add('dotter'); dot.style.animation = `fadeOut${i} 1s linear infinite`; var keyframes = `@keyframes fadeOut${i} {from {transform: translate(0,0) scale(1);opacity: 1;}to {transform: translate(${newRandom()[0]}%,${newRandom()[1]}%) scale(0.1);opacity: 0;}}`; block.appendChild(dot); newKey += keyframes; } css.innerText = newKey; document.getElementsByTagName('body')[0].appendChild(css); }; creator(); function newRandom(){ const angle=Math.random()*360; const dist=Math.random()*2000; const x=Math.cos(angle)*dist; const y=Math.sin(angle)*dist; return [x,y]; };
.dotter{ width: 4px; height: 4px; background: blue; border-radius: 50px; position: absolute; bottom: 50%; left: 50%; transform: translate(0,0); }
<div class="r1"> <img src="star.png"> </div>
const block = document.querySelector('.r1');
const css = document.createElement('style');
// New block Added
const block2 = document.querySelector('.r2');
function creator(){
var newKey = new String;
for(i=0;i<=100;i++){
var dot = document.createElement('div');
dot.classList.add('dotter');
dot.style.animation = `fadeOut${i} 1s linear infinite`;
var keyframes = `@keyframes fadeOut${i} {from {transform: translate(0,0) scale(1);opacity: 1;}to {transform: translate(${newRandom()[0]}%,${newRandom()[1]}%) scale(0.1);opacity: 0;}}`;
block.appendChild(dot);
//Appended New Block
block2.appendChild(dot);
newKey += keyframes;
}
css.innerText = newKey;
document.getElementsByTagName('body')[0].appendChild(css);
};
When I appended the dot
to the block2
element, it only applied to the second one and the sparkles for the first block
dissappeared, why did this happen?当我将
dot
附加到block2
元素时,它只应用于第二个元素并且第一个block
的闪光消失了,为什么会发生这种情况?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.