【问题标题】:jQuery with ReactJs SVG 3D Tag Cloud PluginjQuery 与 ReactJs SVG 3D 标签云插件
【发布时间】:2022-08-16 00:36:05
【问题描述】:

我想在我的 React 网站上实现一个 SVG 3D 标签云插件。我看过这个视频:https://www.youtube.com/watch?v=hzpl-zpkyy4&t=209s 并希望在我的网页右侧实现相同的结果。我在下面附上了 index.js 代码、index.scss 和 script.js 代码。对 ReactJs 来说是全新的,如果我没有提供足够的信息或正确地表达问题,很抱歉。

index.js

import { useEffect, useState } from \'react\'
import Loader from \'react-loaders\'
import AnimatedLetters from \'../AnimatedLetters\'
import React from \'react\';
import ReactDOM from \'react-dom\';
import $ from \'jquery\';
import \'./index.scss\'

const Skills = () => {
  const [letterClass, setLetterClass] = useState(\'text-animate\')

  useEffect(() => {
    setTimeout(() => {
      setLetterClass(\'text-animate-hover\')
    }, 3000)
  }, [])

  useEffect(() => {
    $(document).ready(function(){
      var entries = [
        { label : \'AutoCAD\'},
        { label : \'CSS\'},
        { label : \'C#\'},
        { label : \'C++\'},
        { label : \'Dart\'},
        { label : \'Flutter\'},
        { label : \'HTML\'},
        { label : \'JavaScript\'},
        { label : \'MATLAB\'},
        { label : \'Microsoft Office\'},
        { label : \'Microsoft Visual Basic\'},
        { label : \'MySQL\'},
        { label : \'npm\'},
        { label : \'Photoshop\'},
        { label : \'React\'},
        { label : \'SCSS\'},
        { label : \'Unity 2D\'},
      ];

      var settings = {
        entries : entries,
        width:640,
        height:480,
        raidus:\'65%\',
        radiusMin:75,
        bgDraw:true,
        bgColor:\'#000\',
        opacityOver:1.00,
        opacityOut:0.05,
        opacitySpeed:6,
        fov:800,
        speed:2,
        fontFamily:\'Courier, Arial, sans-serif\',
        fontSize:\'30\',
        fontColor:\'#000\',
        fontWeight:\'bold\',
        fontStyle:\'normal\',
        fontStretch:\'normal\',
        fontToUpperCase:true
      };
      $(\'#tag\').svg3DTagCloud(settings);
    });
  }, []);

  return (
    <>
      <div className=\"container skills-page\">
        <div className=\"text-zone\">
          <h1>
            <AnimatedLetters
              letterClass={letterClass}
              strArray={[\'S\', \'k\', \'i\', \'l\', \'l\', \'s\']}
              idx={15}
            />
          </h1>
          <h2 class=\"quote\">
          \"Jack Of All Trades, Master Of None\"
          </h2>
          <p align=\"LEFT\">
          That would be how I would describe myself. Some experience in 
          <span class=\"tech-tag\"> AutoCAD</span>
          , 
          <span class=\"tech-tag\"> CSS</span>
          , 
          <span class=\"tech-tag\"> C#</span>
          , 
          <span class=\"tech-tag\"> C++</span>
          , 
          <span class=\"tech-tag\"> Dart</span>
          , 
          <span class=\"tech-tag\"> Flutter</span>
          , 
          <span class=\"tech-tag\"> HTML</span>
          , 
          <span class=\"tech-tag\"> JavaScript</span>
          , 
          <span class=\"tech-tag\"> MATLAB</span>
          ,
          <span class=\"tech-tag\"> Microsoft Office</span>
          , 
          <span class=\"tech-tag\"> Microsoft Visual Basic</span>
          , 
          <span class=\"tech-tag\"> MySQL</span>
          , 
          <span class=\"tech-tag\"> npm</span>
          , 
          <span class=\"tech-tag\"> Photoshop</span>
          , 
          <span class=\"tech-tag\"> React</span>
          , 
          <span class=\"tech-tag\"> SCSS</span>
          , and 
          <span class=\"tech-tag\"> Unity 2D</span>.
          </p>
          <p>
          I have exposed myself to various software technologies and am always keen to learn new ones. I put special effort into optimizing my code and ensuring the project\'s objective is delivered.
          </p>
          <p>
          Visit my <a href=\"https://www.linkedin.com/in/deandrehaijielim/\" target=\"_blank\">LinkedIn</a> profile for more details. Also, you can check out my CV on this link. 
          </p>
        </div>
      </div>

      <div id=\"tag\"></div>
      <script src=\"https://code.jquery.com/jquery-3.6.0.js\"></script>
      <script src=\"script.js\"></script>

      <Loader type=\"pacman\" />
    </>
  )
}

export default Skills

索引.scss

.quote {
  color: #fff;
  font-size: 21px;
}

.tech-tag {
    color: #8d8d8d;
  }

a:link {
  color: #8d8d8d;
  background-color: transparent;
  text-decoration: none;
}

a:visited {
  color: gray;
  background-color: transparent;
  text-decoration: none;
}

a:hover {
  color: #9899a5;
  background-color: transparent;
  text-decoration: underline;
}

a:active {
  color: lightyellow;
  background-color: transparent;
  text-decoration: underline;
}

div#tag{
  position: absolute;
  right: 25%;
}

脚本.js

/*
Copyright (c) 2017 Niklas Knaack
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the \"Software\"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

!function(){window.SVG3DTagCloud=function(t,e){var i={entries:[],width:480,height:480,radius:\"70%\",radiusMin:75,bgDraw:!0,bgColor:\"#000\",opacityOver:1,opacityOut:.05,opacitySpeed:6,fov:800,speed:2,fontFamily:\"Arial, sans-serif\",fontSize:\"15\",fontColor:\"#fff\",fontWeight:\"normal\",fontStyle:\"normal\",fontStretch:\"normal\",fontToUpperCase:!1,tooltipFontFamily:\"Arial, sans-serif\",tooltipFontSize:\"15\",tooltipFontColor:\"#fff\",tooltipFontWeight:\"normal\",tooltipFontStyle:\"normal\",tooltipFontStretch:\"normal\",tooltipFontToUpperCase:!1,tooltipTextAnchor:\"left\",tooltipDiffX:0,tooltipDiffY:10,animatingSpeed:.01,animatingRadiusLimit:1.3};if(void 0!==e)for(var o in e)e.hasOwnProperty(o)&&i.hasOwnProperty(o)&&(i[o]=e[o]);if(!i.entries.length)return!1;var n,r,a,s,l,u,c,d=[],m=!0,f={x:0,y:0},p={x:0,y:0,z:0},h={x:0,y:0},v={sx:0,cx:0,sy:0,cy:0},y=Math.PI/180,g=\"http://www.w3.org/2000/svg\",w=1;function A(){window.cancelAnimFrame(c),window.removeEventListener(\"resize\",M),u&&l.removeChild(u),l&&(t.removeChild(l),l.removeEventListener(\"mousemove\",D),delete l)}function b(){(l=document.createElementNS(g,\"svg\")).addEventListener(\"mousemove\",D),t.appendChild(l),i.bgDraw&&((u=document.createElementNS(g,\"rect\")).setAttribute(\"x\",0),u.setAttribute(\"y\",0),u.setAttribute(\"fill\",i.bgColor),l.appendChild(u)),function(){for(var t=!1,e=1,o=i.entries.length+1;e<o;e++){var r=Math.acos(2*e/o-1),a=Math.sqrt(o*Math.PI)*r,s=Math.cos(a)*Math.sin(r),u=Math.sin(a)*Math.sin(r),c=Math.cos(r),m=F(e-1,i.entries[e-1],s,u,c);d.push(m),void 0!==i.entries[e-1].tooltip&&(t=!0)}t&&((n=document.createElementNS(g,\"text\")).setAttribute(\"x\",0),n.setAttribute(\"y\",0),n.setAttribute(\"fill\",i.tooltipFontColor),n.setAttribute(\"font-family\",i.tooltipFontFamily),n.setAttribute(\"font-size\",i.tooltipFontSize),n.setAttribute(\"font-weight\",i.tooltipFontWeight),n.setAttribute(\"font-style\",i.tooltipFontStyle),n.setAttribute(\"font-stretch\",i.tooltipFontStretch),n.setAttribute(\"text-anchor\",i.tooltipTextAnchor),n.textContent=\"\",l.appendChild(n))}(),x(),C(),window.addEventListener(\"resize\",M)}function x(){var e=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,o=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight,n=e,c=o;i.width.toString().indexOf(\"%\")>0||i.height.toString().indexOf(\"%\")>0?(n=Math.round(t.offsetWidth/100*parseInt(i.width)),c=Math.round(n/100*parseInt(i.height))):(n=parseInt(i.width),c=parseInt(i.height)),e<=n&&(n=e),o<=c&&(c=o),s={x:n/2,y:c/2},h.x=i.speed/s.x,h.y=i.speed/s.y,(a=n>=c?c/100*parseInt(i.radius):n/100*parseInt(i.radius))<1&&(a=1),(r=a/2)<i.radiusMin&&(r=i.radiusMin,a=2*r),l.setAttribute(\"width\",n),l.setAttribute(\"height\",c),i.bgDraw&&(u.setAttribute(\"width\",n),u.setAttribute(\"height\",c)),function(t){for(var e=0,i=d.length;e<i;e++)o=d[e],n=t,void 0,void 0,void 0,void 0,r=o.vectorPosition.x-p.x,a=o.vectorPosition.y-p.y,s=o.vectorPosition.z-p.z,l=Math.sqrt(r*r+a*a+s*s),o.vectorPosition.x/=l,o.vectorPosition.y/=l,o.vectorPosition.z/=l,o.vectorPosition.x*=n,o.vectorPosition.y*=n,o.vectorPosition.z*=n;var o,n,r,a,s,l}(r*w)}function F(t,e,o,n,r){var a={};return void 0!==e.label?(a.element=document.createElementNS(g,\"text\"),a.element.setAttribute(\"x\",0),a.element.setAttribute(\"y\",0),a.element.setAttribute(\"fill\",i.fontColor),a.element.setAttribute(\"font-family\",i.fontFamily),a.element.setAttribute(\"font-size\",e.fontSize?e.fontSize:i.fontSize),a.element.setAttribute(\"font-weight\",i.fontWeight),a.element.setAttribute(\"font-style\",i.fontStyle),a.element.setAttribute(\"font-stretch\",i.fontStretch),a.element.setAttribute(\"text-anchor\",\"middle\"),a.element.textContent=i.fontToUpperCase?e.label.toUpperCase():e.label):void 0!==e.image&&(a.element=document.createElementNS(g,\"image\"),a.element.setAttribute(\"x\",0),a.element.setAttribute(\"y\",0),a.element.setAttribute(\"width\",e.width),a.element.setAttribute(\"height\",e.height),a.element.setAttribute(\"id\",\"image_\"+t),a.element.setAttributeNS(\"http://www.w3.org/1999/xlink\",\"href\",e.image),a.diffX=e.width/2,a.diffY=e.height/2),a.link=document.createElementNS(g,\"a\"),a.link.setAttributeNS(\"http://www.w3.org/1999/xlink\",\"xlink:href\",e.url),a.link.setAttribute(\"target\",e.target),a.link.addEventListener(\"mouseover\",P,!0),a.link.addEventListener(\"mouseout\",z,!0),a.link.appendChild(a.element),void 0!==e.tooltip?(a.tooltip=!0,a.tooltipLabel=i.tooltipFontToUpperCase?e.tooltip.toUpperCase():e.tooltip):a.tooltip=!1,a.index=t,a.mouseOver=!1,a.vectorPosition={x:o,y:n,z:r},a.vector2D={x:0,y:0},l.appendChild(a.link),a}function S(t){for(var e=0,i=d.length;e<i;e++){var o=d[e];if(o.element.getAttribute(\"x\")===t.getAttribute(\"x\")&&o.element.getAttribute(\"y\")===t.getAttribute(\"y\"))return o}}function C(){c=requestAnimFrame(C),function(){var t=h.x*f.x-i.speed,e=i.speed-h.y*f.y,o=t*y,n=e*y;v.sx=Math.sin(o),v.cx=Math.cos(o),v.sy=Math.sin(n),v.cy=Math.cos(n);for(var l=0,u=d.length;l<u;l++){var c=d[l];if(m){var p=c.vectorPosition.x,g=c.vectorPosition.y*v.sy+c.vectorPosition.z*v.cy;c.vectorPosition.x=p*v.cx+g*v.sx,c.vectorPosition.y=c.vectorPosition.y*v.cy+c.vectorPosition.z*-v.sy,c.vectorPosition.z=p*-v.sx+g*v.cx}var A,b=i.fov/(i.fov+c.vectorPosition.z);c.vector2D.x=c.vectorPosition.x*b+s.x,c.vector2D.y=c.vectorPosition.y*b+s.y,c.diffX&&c.diffY&&(c.vector2D.x-=c.diffX,c.vector2D.y-=c.diffY),c.element.setAttribute(\"x\",c.vector2D.x),c.element.setAttribute(\"y\",c.vector2D.y),m?(A=(r-c.vectorPosition.z)/a)<i.opacityOut&&(A=i.opacityOut):(A=parseFloat(c.element.getAttribute(\"opacity\")),c.mouseOver?A+=(i.opacityOver-A)/i.opacitySpeed:A+=(i.opacityOut-A)/i.opacitySpeed),c.element.setAttribute(\"opacity\",A*(1-(w-1)/(i.animatingRadiusLimit-1)))}d=d.sort(function(t,e){return e.vectorPosition.z-t.vectorPosition.z})}()}function P(t){m=!1;var e,o=S(t.target);!function(t){for(var e=0,i=d.length;e<i;e++){var o=d[e];o.index===t.index?o.mouseOver=!0:o.mouseOver=!1}}(o),o.tooltip&&(e=o).tooltip&&(n.setAttribute(\"x\",e.vector2D.x-i.tooltipDiffX),n.setAttribute(\"y\",e.vector2D.y-i.tooltipDiffY),n.textContent=i.tooltipFontToUpperCase?e.tooltipLabel.toUpperCase():e.tooltipLabel,n.setAttribute(\"opacity\",1))}function z(t){m=!0;var e=S(t.target);e.tooltip&&n.setAttribute(\"opacity\",0)}function D(t){var e,i;e=t,i=l.getBoundingClientRect(),f={x:e.clientX-i.left,y:e.clientY-i.top}}function M(t){x()}function T(t){w=Math.min(Math.max(t,1),i.animatingRadiusLimit),x()}window.requestAnimFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(t){return window.setTimeout(t,1e3/60)},window.cancelAnimFrame=window.requestAnimationFrame?window.cancelAnimationFrame:window.webkitRequestAnimationFrame?window.webkitCancelAnimationFrame:window.mozRequestAnimationFrame?window.mozCancelAnimationFrame:window.clearTimeout;var E=!1,O=!1,L=!1;function k(){(L=w<i.animatingRadiusLimit)?(T(w+i.animatingSpeed),requestAnimFrame(k)):\"function\"==typeof E&&(E(),E=!1)}function q(){(L=w>1)?(T(w-i.animatingSpeed),requestAnimFrame(q)):\"function\"==typeof O&&(O(),O=!1)}b(),this.destroy=A,this.animOut=function(t){L||(w=1,E=t,k())},this.animIn=function(t){L||(w=i.animatingRadiusLimit,O=t,q())},this.setEntries=function(t){A(),i.entries=t,b()}}}(),\"undefined\"!=typeof jQuery&&function(t){t.fn.svg3DTagCloud=function(e){var i=arguments;return this.each(function(){if(t.data(this,\"plugin_SVG3DTagCloud\")){var o=t.data(this,\"plugin_SVG3DTagCloud\");o[e]?o[e].apply(this,Array.prototype.slice.call(i,1)):t.error(\"Method \"+e+\" does not exist on jQuery.svg3DTagCloud\")}else t.data(this,\"plugin_SVG3DTagCloud\",new SVG3DTagCloud(this,e))})}}(jQuery);

    标签: jquery reactjs


    【解决方案1】:

    从“反应”导入反应,{ useEffect,useRef } 从“标签云”导入标签云

    常量标签 = [ 'JavaScript', 'CSS', 'HTML', 'Vscode', 'XD', '反应', 'Python', 'Linux', '混帐', '扑', '镖', '火力基地', 'SASS', 'JSON', '无花果', ]

    函数应用程序(){ const IsTagCloudLoaded = useRef(false)

    useEffect(() => {
        if (IsTagCloudLoaded.current) return
    
        TagCloud('.content', Tags, {
            radius: 250,
            maxSpeed: 'fast',
            initSpeed: 'fast',
            direction: 135,
            keep: true,
        })
    
        IsTagCloudLoaded.current = true
    }, [])
    
    return (
        <div className='About' id='About'>
            <div className='left'>
                <div className='heading'>
                    <h1>ABOUT</h1>
                </div>
            </div>
            <div className='right'>
                <div className='content' />
            </div>
        </div>
    )
    

    }

    导出默认应用

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2012-08-21
      相关资源
      最近更新 更多