简体   繁体   English

Polymer 3.0自定义元素-如何使用JavaScript与SVG进行交互

[英]Polymer 3.0 Custom Element - How to Use javascript to interact with SVG

I am new on Polymer, so I'm learning while developing. 我是Polymer的新手,所以我在学习的同时学习。 Right now I'm using the latest version, Polymer 3.0. 现在,我正在使用最新版本的Polymer 3.0。 I already had a working frontpage on this site I'm working on. 我正在处理的这个网站上已经有一个工作版首页。 While I was trying to make everything work on polymer, I got stuck with making the javascript work. 当我尝试使所有内容在聚合物上都能正常工作时,我一直无法使javascript工作。

On the original site, I was using this code 在原始网站上,我正在使用此代码

index.html 的index.html

                <div class="year-overview">

                    <div class="ano-lectivo">
                        <svg class="progress-ring" width="50%" height="50%" opacity="0" viewBox="0 0 42 42">
                            <circle class="progress-ring__circle" stroke="white" fill="transparent" r="15.91549430918954" cx="21" cy="21"></circle>
                            <text x="50%" y="53%" class="circle-text" text-anchor="middle">
                                <tspan class="currentDay">35</tspan>
                                <tspan class="daysLeft">/ 300</tspan>
                                <tspan x="50%" y="60%">Días Lectivos</tspan>
                            </text>
                        </svg>
                    </div>

And this is the javascript file: 这是javascript文件:

        function circleCircus(ringClass, circleClass, ringProgress) {
                var circle = document.querySelector(circleClass);
                var radius = circle.r.baseVal.value;
                var circumference = radius * 2 * Math.PI;
                var circleSVG = document.querySelector(ringClass); //For changing the opacity and not showing the circle before it loads

                circle.style.strokeDasharray = `${circumference} ${circumference}`;
                circle.style.strokeDashoffset = `${circumference}`;

                function setProgress(percent) {
                    const offset = circumference - percent / 100 * circumference;
                    circle.style.strokeDashoffset = offset;
                }

            setTimeout(function() {circleSVG.setAttribute("opacity", "100%");}, 1000); //Changing the circle opacity
            setTimeout(function() { setProgress(ringProgress); }, 2000); //Changin the value in order to animate
        }

        //----------------------Progress Bar 1------------------------------------
        circleCircus('.progress-ring', '.progress-ring__circle', 50);

Now that I'm working on Polymer I was creating a custom element for the donut chart, but I don't how to use or where to put the function that makes the strokeDasharray and the strokeDashoffset work correctly: 现在,我正在使用Polymer,我正在为甜甜圈图创建一个自定义元素,但是我不知道如何使用或放置使strokeDasharray和strokeDashoffset正常工作的函数:

            import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';

            class DonutChart extends PolymerElement {
                static get template() {
                    return html`
                        <style>

                            .progress-ring__circle {
                                stroke-width: 3;
                                stroke: #000;
                                position: relative;
                                transition: stroke-dashoffset 0.35s, stroke-width 500ms ease-out, stroke 500ms ease-out;
                                -webkit-transition: stroke-dashoffset 0.35s, stroke-width 500ms ease-out, stroke 500ms ease-out; /* For Safari 3.1 to 6.0 */
                                transform: rotate(-90deg);
                                transform-origin: 50% 50%;
                            }

                            .currentDay {
                                font-size: 40%;
                                overflow: visible;
                                -webkit-transition: font-size 500ms ease-out; /* For Safari 3.1 to 6.0 */
                                transition: font-size 500ms ease-out;
                            }

                            .daysLeft {
                                font-size: 40%;
                                -webkit-transition: font-size 500ms, color 500ms; /* For Safari 3.1 to 6.0 */
                                transition: font-size 500ms, color 500ms;
                            }

                            .description {
                                font-size: 20%;
                            }


                            .circle-text {
                                fill: #000;
                            }

                            .progress-ring {
                                display: block;
                                position: relative;
                                margin-left: auto;
                                margin-right: auto;
                            }

                            svg:hover .currentDay {
                                font-size: 60%;
                            }

                            svg:hover .daysLeft {
                                font-size: 10%;
                            }


                            svg:hover .progress-ring__circle {
                                stroke-width: 1;
                                stroke: #5a64ed;
                            }        

                        </style>

                        <svg class="progress-ring" width="100%" height="100%" viewBox="0 0 42 42">
                            <circle class="progress-ring__circle" stroke="white" fill="transparent" r="15.91549430918954" cx="21" cy="21"></circle>
                            <text x="50%" y="53%" class="circle-text" text-anchor="middle">
                            <tspan class="numbers" x="54%" y="50%"><tspan class="currentDay">35</tspan><tspan class="daysLeft">/300</tspan></tspan>

                                <tspan x="50%" y="60%" class="description">Días Lectivos</tspan>
                            </text>
                        </svg>

                    `;
                }

                constructor() {
                    super();
                }

                    var circle = this.shadowRoot.querySelector('.progress-ring__circle');
                    var radius = circle.r.baseVal.value;
                    var circumference = radiius * 2 * Math.PI;
                    var circleSVG = this.shadowRoot.querySelector('.progress-ring');

                    circle.style.strokeDasharray = `${circumference} ${circumference}`;
                    circle.style.strokeDashoffset = `${circumference}`;

                    function setProgress(percent) {
                        const offset = circumference - percent / 100 * circumference;
                        circle.style.strokeDashoffset = offset;
                    }
                    setProgress(79);



            }

            customElements.define('donut-chart', DonutChart);

Here is my original code and what I'm trying to accomplish in the custom element: https://codepen.io/maganalexis/pen/ePOxYX 这是我的原始代码,也是我在自定义元素中要完成的工作: https : //codepen.io/maganalexis/pen/ePOxYX

Thank you for helping out. 感谢您的帮助。

After reading a lot of documents I found out the solution, I had to call the ready method and put my code under it: 阅读了大量文档后,我找到了解决方案,我不得不调用ready方法并将代码放在其中:

            import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';

            class DonutChart extends PolymerElement {
                static get template() {
                    return html`
                        <style>

                            .progress-ring__circle {
                                stroke-width: 3;
                                stroke: #000;
                                position: relative;
                                transition: stroke-dashoffset 0.35s, stroke-width 500ms ease-out, stroke 500ms ease-out;
                                -webkit-transition: stroke-dashoffset 0.35s, stroke-width 500ms ease-out, stroke 500ms ease-out; /* For Safari 3.1 to 6.0 */
                                transform: rotate(-90deg);
                                transform-origin: 50% 50%;
                            }

                            .currentDay {
                                font-size: 40%;
                                overflow: visible;
                                -webkit-transition: font-size 500ms ease-out; /* For Safari 3.1 to 6.0 */
                                transition: font-size 500ms ease-out;
                            }

                            .daysLeft {
                                font-size: 40%;
                                -webkit-transition: font-size 500ms, color 500ms; /* For Safari 3.1 to 6.0 */
                                transition: font-size 500ms, color 500ms;
                            }

                            .description {
                                font-size: 20%;
                            }


                            .circle-text {
                                fill: #000;
                            }

                            .progress-ring {
                                display: block;
                                position: relative;
                                margin-left: auto;
                                margin-right: auto;
                            }

                            svg:hover .currentDay {
                                font-size: 60%;
                            }

                            svg:hover .daysLeft {
                                font-size: 10%;
                            }


                            svg:hover .progress-ring__circle {
                                stroke-width: 1;
                                stroke: #5a64ed;
                            }        

                        </style>

                        <svg class="progress-ring" on-load="circleCircus" width="100%" height="100%" viewBox="0 0 42 42">
                            <circle class="progress-ring__circle" stroke="white" fill="transparent" r="15.91549430918954" cx="21" cy="21"></circle>
                            <text x="50%" y="53%" class="circle-text" text-anchor="middle">
                            <tspan class="numbers" x="54%" y="50%"><tspan class="currentDay">[[progressNum]]</tspan><tspan class="daysLeft">[[totalNum]]</tspan></tspan>

                                <tspan x="50%" y="60%" class="description">[[lowDescription]]</tspan>
                            </text>
                        </svg>

                    `;
                }

                static get properties() {
                    return {
                        progressNum: {
                            type: Number
                        },
                        totalNum: {
                            type: String
                        },
                        lowDescription: {
                            type: String
                        }
                    };
                }

                constructor() {
                    super();
                }




                ready(){
                    super.ready();

                    var circle = this.shadowRoot.querySelector('.progress-ring__circle');
                    var radius = circle.r.baseVal.value;
                    var circumference = radius * 2 * Math.PI;
                    var circleSVG = this.shadowRoot.querySelector('.progress-ring'); //For changing the opacity and not showing the circle before it loads
                    var proNum = `${this.progressNum}`;

                    circle.style.strokeDasharray = `${circumference} ${circumference}`;
                    circle.style.strokeDashoffset = `${circumference}`;

                    function setProgress(percent) {
                        const offset = circumference - percent / 100 * circumference;
                        circle.style.strokeDashoffset = offset;
                    }

                    setTimeout(function() {circleSVG.setAttribute("opacity", "100%");}, 1000); //Changing the circle opacity
                    setTimeout(function() { setProgress(proNum); }, 2000); //Changin the value in order to animate
                }


            }

            customElements.define('donut-chart', DonutChart);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何与javascript元素进行交互 - How to interact with a javascript element 如何将数据从JavaScript代码传递到Polymer自定义元素? - How to pass data from JavaScript code to Polymer custom element? 如何将javascript代码结果返回到聚合物自定义元素的 <template> ? - How to return javascript code result to the polymer custom element's <template>? 如何使disqus注释javascript代码在聚合物自定义元素中工作 - How to make a disqus comments javascript code to work in a polymer custom element 使用Polymer 3.0分别定义自定义元素以根据需求使用它们 - Defining Custom Elements separately with Polymer 3.0 to use them based on demand 使用本机节点名称作为Polymer自定义元素? - Use native node name for a Polymer custom element? 如何将非模块(!)JavaScript 导入 Polymer 3.0 - How to import non-module(!) JavaScript into Polymer 3.0 无法使用Javascript访问Polymer custom-element中的Canvas Element - Cannot access Canvas Element inside Polymer custom-element with Javascript 聚合物1.0:如何使该自定义元素具有声明性? - Polymer 1.0: how to make this custom element declarative? 如何在Polymer上创建自定义元素的实例 - How to create the instance of custom element on Polymer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM