简体   繁体   English

通过 JavaScript(Django,Python)创建时,圆圈未显示在网页中

[英]Circles not showing up in webpage when created via JavaScript (Django, Python)

I have made a page in my Django webapp that displays an image and allows the user to click on the image and should place a circle on top of the image where the user clicked.我在我的 Django webapp 中创建了一个页面,该页面显示图像并允许用户单击图像,并且应该在用户单击的图像顶部放置一个圆圈。 The following code is able to get the location of the mouse click on the image, and adds a tag to the html in the right place, but it won't actually show up on the page.下面的代码可以获取鼠标在图片上点击的位置,并在 html 的正确位置添加标签,但实际上并不会显示在页面上。 If I hard code a circle into the html, it shows up just fine.如果我在 html 中硬编码一个圆圈,它会显示得很好。 It also seems like the mouse is on a different coordinate system than the SVG element... I have tried event.x, event.pageX, and event.clientX but noticed no difference.似乎鼠标位于与 SVG 元素不同的坐标系上……我尝试了 event.x、event.pageX 和 event.clientX,但没有发现任何区别。 Even if I hard code the position of the circle, it won't show up on the click即使我对圆圈的 position 进行硬编码,它也不会在点击时显示

html: html:

{% extends 'main/base.html' %}

{% block head_content %}
{% load static %}
<script type="text/javascript" src="{% static 'js/click.js' %}"></script>
{% endblock %}

{% block content %}

<div class="my-div">

    <div>
        <h1>The Page!</h1>
    </div>
    <form enctype="multipart/form-data" method="POST" action="/mysite/nextpage/">
        {% csrf_token %}

        <svg id="svg">
            <image href="data:image/png;base64,{{ my_img }}"/>
            <!-- Placing a circle manually works just fine! -->
            <circle cx='50' cy='150' r='50' fill='red'/>
            
        </svg>

        <input type="submit" name="submit" class="submit-btn"></input>
    </form>

</div>

{% endblock %}

Javascript: Javascript:

this.window.addEventListener('DOMContentLoaded', (event) => {
    const svgElem = this.document.getElementsByTagName("svg")

    if (svgElem != undefined && svgElem.length != 0) {
        const svg = svgElem[0]
        const image = svg.firstChild
        svg.onclick = function(event) {
            
            var newCircle = document.createElement('circle')
            newCircle.setAttribute('cx', event.clientX)
            newCircle.setAttribute('cy', event.clientY)
            newCircle.setAttribute('r', '50')
            newCircle.setAttribute('fill', 'red')
            svg.append(newCircle)
        }
    }
});

Thanks to @enxaneta in the comments for suggesting createElementNS, and thanks to this post (also credit @enxaneta) for helping translate the coordinates, I got the code to work.感谢评论中的 @enxaneta 建议 createElementNS,感谢这篇文章(也感谢 @enxaneta)帮助翻译坐标,我得到了代码工作。

this.window.addEventListener('DOMContentLoaded', (event) => {
    const svgElem = this.document.getElementsByTagName("svg")

    if (svgElem != undefined && svgElem.length != 0) {
        const svg = svgElem[0]
        const image = svg.firstChild
        svg.onclick = function(event) {
            
            var newCircle = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
            let m = mousePositionSVG(event)
            newCircle.setAttribute('cx', m.x)
            newCircle.setAttribute('cy', m.y)
            newCircle.setAttribute('r', '50')
            newCircle.setAttribute('fill', 'red')
            svg.append(newCircle)
        }
    }
});

function mousePositionSVG(e) {
    let svg = document.querySelector('svg')
    var p = svg.createSVGPoint()
    p.x = e.clientX
    p.y = e.clientY
    var ctm = svg.getScreenCTM().inverse();
    var p =  p.matrixTransform(ctm);
    return p;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM