欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS?簡單實現(xiàn)拖拽評星的示例代碼

 更新時間:2023年05月06日 08:28:21   作者:頭疼腦脹的代碼搬運工  
本文主要介紹了JS?簡單實現(xiàn)拖拽評星,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

廢話開篇:通過 canvas 簡單拖拽評星,主要是通過個人的理解去實現(xiàn)這樣的一個效果。

一、實現(xiàn)效果

html

<div class="main">
        <div class="score_container">
          <canvas id="canvas" height="100"></canvas>
          <div id="score" class="score">評分:0</div>
        </div>
    </div>

css

.main {
    display: flex;
    flex-direction: row;
    justify-content: start;
    align-items: flex-start;
    padding-top: 20px;
    padding-left: 20px;
}
.score_container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-top: 20px;
}
.score {
    margin-top: 20px;
}

js

// 星星數(shù)據(jù)對象
? ? class Pentagram{
? ? ? ? points = []
? ? ? ? minX = 0
? ? ? ? maxX = 0
? ? ? ? deg = (Math.PI / 180)
? ? ? ? score = 0
? ? ? ? constructor(index,r,center){
? ? ? ? ? ? this.savePentagramData(index,r,center)
? ? ? ? }
? ? ? ? // 繪制星星
? ? ? ? savePentagramData(currentPentagramIndex,r,{x,y}){
? ? ? ? ? ? // 它對應(yīng)的整數(shù)分數(shù)
? ? ? ? ? ? this.score = currentPentagramIndex + 1
? ? ? ? ? ? // 工具函數(shù)
? ? ? ? ? ? let cos = (d)=>{ return Math.cos(d * this.deg) }
? ? ? ? ? ? let sin = (d)=>{ return Math.sin(d * this.deg) }
? ? ? ? ? ? let tan = (d)=>{ return Math.tan(d * this.deg) }
? ? ? ? ? ? let square = (num)=> { return Math.pow(num,2) }
? ? ? ? ? ? // 外邊比例
? ? ? ? ? ? let t = 1 / ((1 + square(tan(18))) / (3 - square(tan(18))))
? ? ? ? ? ? this.points = [
? ? ? ? ? ? ? ? [0,1],
? ? ? ? ? ? ? ? [t*cos(54),t*sin(54)],
? ? ? ? ? ? ? ? [cos(18),sin(18)],
? ? ? ? ? ? ? ? [t*cos(18),-t*sin(18)],
? ? ? ? ? ? ? ? [cos(54),-sin(54)],
? ? ? ? ? ? ? ? [0,-t],
? ? ? ? ? ? ? ? [-cos(54),-sin(54)],
? ? ? ? ? ? ? ? [-t*cos(18),-t*sin(18)],
? ? ? ? ? ? ? ? [-cos(18),sin(18)],
? ? ? ? ? ? ? ? [-t*cos(54),t*sin(54)],
? ? ? ? ? ? ? ? [0,1],
? ? ? ? ? ? ]
? ? ? ? ? ? this.points.forEach((point,index)=>{
? ? ? ? ? ? ? ? point[0] = x + point[0] * (r / t)
? ? ? ? ? ? ? ? point[1] = y + point[1] * (r / t)
? ? ? ? ? ? ? ? if(index == 7) {
? ? ? ? ? ? ? ? ? ? // 最右側(cè)的點
? ? ? ? ? ? ? ? ? ? this.minX = point[0]
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(index == 3) {
? ? ? ? ? ? ? ? ? ? // 最左側(cè)的點
? ? ? ? ? ? ? ? ? ? this.maxX = point[0]
? ? ? ? ? ? ? ? }
? ? ? ? ? ? })
? ? ? ? }
? ? }
? ? // 星星管理器
? ? class PentagramManage{
? ? ? ? canvas = null//畫板相關(guān)
? ? ? ? context = null
? ? ? ? pentagramNum = 1//星星個數(shù)
? ? ? ? isMouseDown = false//鼠標(biāo)是否按下
? ? ? ? progress = 0//當(dāng)前評分位置
? ? ? ? pentagramRadius = 15//星星半徑
? ? ? ? pentagramSep = 10//星星間間隔
? ? ? ? pentagrams = []//記錄每一個星星對象
? ? ? ? constructor(pentagramNum){
? ? ? ? ? ? this.pentagramNum = pentagramNum
? ? ? ? ? ? this.initData()
? ? ? ? ? ? this.draw()
? ? ? ? ? ? this.bindMouseEvent()
? ? ? ? }
? ? ? ? // 初始化
? ? ? ? initData(){
? ? ? ? ? ? this.canvas = document.getElementById('canvas')
? ? ? ? ? ? for(let i = 0;i < this.pentagramNum;i ++){
? ? ? ? ? ? ? ? let pentagram = ?new Pentagram(i,this.pentagramRadius,{x:35 + i * (this.pentagramRadius * 2 + this.pentagramSep),y:(this.canvas.height / 2.0) })
? ? ? ? ? ? ? ? this.pentagrams.push(pentagram)
? ? ? ? ? ? ? ? if(i == this.pentagramNum - 1){
? ? ? ? ? ? ? ? ? ? this.canvas.width = pentagram.maxX + 20
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? this.context = this.canvas.getContext('2d');
? ? ? ? ? ? this.context.fillStyle='white';
? ? ? ? }
? ? ? ? //繪制
? ? ? ? draw(){
? ? ? ? ? ? this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
? ? ? ? ? ? this.drawBottomPlate()
? ? ? ? ? ? let hook = ()=>{?
? ? ? ? ? ? ? ? this.pentagrams.forEach((pentagramItem)=>{
? ? ? ? ? ? ? ? ? ? this.drawPentagram(pentagramItem)
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? ? ? this.drawHollowOut(hook)
? ? ? ? ? ? this.drawStrokeHollowOut(hook)
? ? ? ? }
? ? ? ? // 繪制底色
? ? ? ? drawBottomPlate(){
? ? ? ? ? ? this.context.save()
? ? ? ? ? ? this.context.fillStyle= 'rgb(247,190,80)';
? ? ? ? ? ? this.context.beginPath();
? ? ? ? ? ? this.context.rect(0, 0, this.progress, this.canvas.height);
? ? ? ? ? ? this.context.closePath();
? ? ? ? ? ? this.context.fill();
? ? ? ? ? ? this.context.restore()
? ? ? ? }
? ? ? ? // 繪制鏤空五角星
? ? ? ? drawHollowOut(hook){
? ? ? ? ? ? this.context.beginPath();
? ? ? ? ? ? this.context.rect(0, 0, this.canvas.width, this.canvas.height);
? ? ? ? ? ? hook()
? ? ? ? ? ? this.context.closePath();
? ? ? ? ? ? this.context.fill();
? ? ? ? }
? ? ? ? // 繪制五星邊框
? ? ? ? drawStrokeHollowOut(hook){
? ? ? ? ? ? this.context.save();
? ? ? ? ? ? this.context.strokeStyle = 'rgb(247,190,80)';
? ? ? ? ? ? // this.context.stroke.width = 1
? ? ? ? ? ? this.context.beginPath();
? ? ? ? ? ? this.context.rect(0, 0, this.canvas.width, this.canvas.height);
? ? ? ? ? ? hook()
? ? ? ? ? ? this.context.closePath();
? ? ? ? ? ? this.context.stroke();
? ? ? ? ? ? this.context.restore();
? ? ? ? }
? ? ? ? // 繪制星星
? ? ? ? drawPentagram(pentagramItem){
? ? ? ? ? ? pentagramItem.points.forEach((point,index)=>{
? ? ? ? ? ? ? ? eval('this.context.' + (index == 0 ? 'moveTo(' : 'lineTo(') + '...point)')
? ? ? ? ? ? })
? ? ? ? }
? ? ? ? // 綁定鼠標(biāo)事件
? ? ? ? bindMouseEvent(){
? ? ? ? ? ? document.onmousemove = (event)=>{
? ? ? ? ? ? ? ? if(this.isMouseDown){
? ? ? ? ? ? ? ? ? ? let { left } = this.getElementPosition(document.getElementById('canvas'))
? ? ? ? ? ? ? ? ? ? this.progress = event.clientX - left
? ? ? ? ? ? ? ? ? ? this.draw()
? ? ? ? ? ? ? ? ? ? this.getCurrentScore()
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //鼠標(biāo)按下事件
? ? ? ? ? ? document.onmousedown = (event)=>{
? ? ? ? ? ? ? ? this.isMouseDown = true ? ? ? ? ? ??
? ? ? ? ? ? ? ? let { left } = this.getElementPosition(document.getElementById('canvas'))
? ? ? ? ? ? ? ? ? ? this.progress = event.clientX - left
? ? ? ? ? ? ? ? ? ? this.draw()
? ? ? ? ? ? ? ? ? ? this.getCurrentScore()
? ? ? ? ? ? }
? ? ? ? ? ? //鼠標(biāo)抬起事件
? ? ? ? ? ? document.onmouseup = ()=>{
? ? ? ? ? ? ? ? this.isMouseDown = false
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 計算分數(shù)
? ? ? ? getCurrentScore(){
? ? ? ? ? ? let score = 0
? ? ? ? ? ? let firstPentagram = this.pentagrams.find((pentagram)=>{
? ? ? ? ? ? ? ? return this.progress <= pentagram.maxX
? ? ? ? ? ? })
? ? ? ? ? ? if(firstPentagram){
? ? ? ? ? ? ? ? let float = (Math.floor(((this.progress - firstPentagram.minX) / (firstPentagram.maxX - firstPentagram.minX)) * 10)) / 10
? ? ? ? ? ? ? ? float = float > 0 ? float : 0
? ? ? ? ? ? ? ? score = (firstPentagram.score - 1) + float
? ? ? ? ? ? ? ? document.getElementById('score').innerHTML = "評分:" + score
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? document.getElementById('score').innerHTML = "評分:" + this.pentagrams.length
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // dom在瀏覽器的位置
? ? ? ? getElementPosition(element){
? ? ? ? ? ? let top = element.offsetTop
? ? ? ? ? ? let left = element.offsetLeft
? ? ? ? ? ? let width = element.offsetWidth
? ? ? ? ? ? let height = element.offsetHeight
? ? ? ? ? ? var currentParent = element.offsetParent;
? ? ? ? ? ? while (currentParent !== null) { ? ? ?
? ? ? ? ? ? ? ? top += currentParent.offsetTop
? ? ? ? ? ? ? ? left += currentParent.offsetLeft
? ? ? ? ? ? ? ? currentParent = currentParent.offsetParent
? ? ? ? ? ? }
? ? ? ? ? ? return {top,left,width,height}
? ? ? ? }
? ? }
? ? var pentagram = new PentagramManage(4)

二、總結(jié)與思考

上層無鏤空

上層有鏤空

通過 canvas 實現(xiàn)一層鏤空五角星層,再在底層添加一個進度層,這樣在拖動的時候就能通過拖拽的位置進行數(shù)據(jù)處理,從而計算出星級數(shù)。代碼拙劣,大神勿笑 

到此這篇關(guān)于JS 簡單實現(xiàn)拖拽評星的示例代碼的文章就介紹到這了,更多相關(guān)JS拖拽評星內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • JS中圖片壓縮的方法小結(jié)

    JS中圖片壓縮的方法小結(jié)

    這篇文章主要介紹了JS中圖片壓縮的方法,包括等比壓縮圖片的方法,需要的朋友可以參考下
    2017-11-11
  • JS實現(xiàn)運動緩沖效果的封裝函數(shù)示例

    JS實現(xiàn)運動緩沖效果的封裝函數(shù)示例

    這篇文章主要介紹了JS實現(xiàn)運動緩沖效果的封裝函數(shù),涉及JavaScript時間函數(shù)與數(shù)值運算相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • ES6中Proxy代理用法實例淺析

    ES6中Proxy代理用法實例淺析

    這篇文章主要介紹了ES6中Proxy代理用法,結(jié)合實例形式簡單分析了Proxy代理的概念、功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下
    2017-04-04
  • 如何消除inline-block屬性帶來的標(biāo)簽間間隙

    如何消除inline-block屬性帶來的標(biāo)簽間間隙

    這篇文章主要介紹了如何消除inline-block屬性帶來的標(biāo)簽間間隙的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • webpack打包node.js后端項目的方法

    webpack打包node.js后端項目的方法

    本篇文章主要介紹了webpack打包node.js后端項目的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Layui實現(xiàn)帶查詢條件的分頁

    Layui實現(xiàn)帶查詢條件的分頁

    這篇文章主要為大家詳細介紹了Layui實現(xiàn)帶查詢條件的分頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • js實現(xiàn)iframe自動自適應(yīng)高度的方法

    js實現(xiàn)iframe自動自適應(yīng)高度的方法

    這篇文章主要介紹了js實現(xiàn)iframe自動自適應(yīng)高度的方法,涉及javascript操作iframe框架的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-02-02
  • JavaScript基礎(chǔ)知識之方法匯總結(jié)

    JavaScript基礎(chǔ)知識之方法匯總結(jié)

    本文給大家分享了javascript基礎(chǔ)知識,包括數(shù)組的方法,函數(shù)的方法,數(shù)字的方法,對象的方法,字符串的方法,常規(guī)方法,正則表達式方法,本文介紹的非常詳細,具有參考價值特此分享供大家參考
    2016-01-01
  • 性能優(yōu)化篇之Webpack構(gòu)建速度優(yōu)化的建議

    性能優(yōu)化篇之Webpack構(gòu)建速度優(yōu)化的建議

    這篇文章主要介紹了性能優(yōu)化篇之Webpack構(gòu)建速度優(yōu)化的建議,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • js如何實現(xiàn)設(shè)計模式中的模板方法

    js如何實現(xiàn)設(shè)計模式中的模板方法

    都知道在js中如果定義兩個相同名稱的方法,前一個方法就會被后一個方法覆蓋掉,使用此特點就可以實現(xiàn)模板方法,感興趣的朋友可以了解下本文哈
    2013-07-07

最新評論