JS 코드를 사용하여 사격 효과 만들기

php中世界最好的语言
풀어 주다: 2018-03-13 16:37:33
원래의
3451명이 탐색했습니다.

이번에는 JS 코드를 사용하여 사격 효과를 만드는 방법을 보여 드리겠습니다. JS 코드를 사용하여 사격 효과를 만드는 주의 사항은 다음과 같습니다.

구현 원리

1. 표시 사격 요소의 위치 속성을 상대값으로 설정합니다
2. 위치 속성을 절대값으로 설정하고, 왼쪽은 표시 폭
3의 상위 값을 임의로 설정합니다. 사격 요소
4. 사격 요소의 이동 속도를 무작위로 생성하고 왼쪽 값을 수정합니다

무작위 색상

첫 번째 구현 let color = '#' + Math.floor(Math.random() * 0xffffff).toString(16) ;

두 번째 구현 let color = '#' + Math.floor(Math.random() * 16777215).toString(16);

세 번째 구현 let r = Math.floor(Math.random()*256 ); let g = Math.floor(Math.random()*256);let b = Math.floor(Math.random()*256); let color = `rgb(${r},${g}, ${b}) `;

Random rate

50 * +Math.random().toFixed(2)
로그인 후 복사

code

//html

<div class="container">
    <div id="content" class="content"></div>
    <div class="content-opt">
        <div id="content-text" class="content-text"></div>
        <div class="content-input">
            <input id="text" type="text">
            <button id="send">发送</button>
        </div>
    </div>
</div>
로그인 후 복사

//css

* {
    box-sizing: border-box;
    outline: none;
}
p {
    margin: .5em;
    word-break: break-all;
}
.container {
    position: relative;
    width: 700px;
    height: 500px;
    margin: auto;
    padding-right: 200px;
}
.content {
    width: 100%;
    height: 100%;
    border: 1px solid #ccc;}
.content-opt {
    position: absolute;
    top: 0;
    right: 0;
    width: 200px;
    height: 100%;
}
.content-text {
    height: calc(100% - 30px);
    margin-bottom: 30px;
    border: 1px solid #ccc;
    overflow: auto;
}
.content-input {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 30px;
    border: 1px solid #ccc;}
.content-input input {
    width: 70%;
    padding: 2px;
    border-radius: 5px;
}
.content-input button {
    padding: 3px 10px;
    border: none;
    border-radius: 5px;
    background: rgb(90, 154, 214);
}
로그인 후 복사

//js

(function () {
    class Barrage {
        constructor(id) {
            this.domList = [];
            this.dom = document.querySelector(&#39;#&#39; + id);            if (this.dom.style.position == &#39;&#39; || this.dom.style.position == &#39;static&#39;) {
                this.dom.style.position = &#39;relative&#39;;
            }
            this.dom.style.overflow = &#39;hidden&#39;;            let rect = this.dom.getBoundingClientRect();
            this.domWidth = rect.right - rect.left;
            this.domHeight = rect.bottom - rect.top;
        }
        shoot(text) {            let div = document.createElement(&#39;div&#39;);
            div.style.position = &#39;absolute&#39;;
            div.style.left = this.domWidth + &#39;px&#39;;
            div.style.top = (this.domHeight - 20) * +Math.random().toFixed(2) + &#39;px&#39;;
            div.style.whiteSpace = &#39;nowrap&#39;;
            div.style.color = &#39;#&#39; + Math.floor(Math.random() * 0xffffff).toString(16);
            div.innerText = text;
            this.dom.appendChild(div);            let roll = (timer) => {                let now = +new Date();
                roll.last = roll.last || now;
                roll.timer = roll.timer || timer;                let left = div.offsetLeft;                let rect = div.getBoundingClientRect();                if (left < (rect.left - rect.right)) {
                    this.dom.removeChild(div);
                } else {                    if (now - roll.last >= roll.timer) {
                        roll.last = now;
                        left -= 3;
                        div.style.left = left + &#39;px&#39;;
                    }
                    requestAnimationFrame(roll);
                }
            }
            roll(50 * +Math.random().toFixed(2));
        }
    }    let barage = new Barrage(&#39;content&#39;);    function appendList(text) {        let p = document.createElement(&#39;p&#39;);
        p.innerText = text;
        document.querySelector(&#39;#content-text&#39;).appendChild(p);
    }
    document.querySelector(&#39;#send&#39;).onclick = () => {        let text = document.querySelector(&#39;#text&#39;).value;
        barage.shoot(text);
        appendList(text);
    };
    const textList = [&#39;弹幕&#39;, &#39;666&#39;, &#39;233333333&#39;, &#39;javascript&#39;, &#39;html&#39;, &#39;css&#39;, &#39;前端框架&#39;, &#39;Vue&#39;, &#39;React&#39;, &#39;Angular&#39;,        &#39;测试弹幕效果&#39;
    ];
    textList.forEach((s) => {
        barage.shoot(s);
        appendList(s);
    })
})()
로그인 후 복사

사례를 읽어보니 방법을 마스터하신 것 같습니다. 이 기사에서는 더 흥미로운 내용에 주목해 주세요. PHP 중국어 웹사이트의 기타 관련 기사도 있습니다!

추천 자료:

H5 캔버스를 사용하여 사격 효과 만들기

H5 캔버스를 사용하여 공포 애니메이션 만들기

위 내용은 JS 코드를 사용하여 사격 효과 만들기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!