Answer the question
In order to leave comments, you need to log in
How to transfer mouse coordinates from canvas to database in django?
Hello!
I fight the third day, please, prompt. I can not solve the problem of passing coordinates from the canvas to the django database and back.
Here is such a drawing of schemes on django. For now, we are just drawing lines, but these lines must be saved in the database. That is, I draw, it should be immediately saved in the database:
canvas.js:
var ctx = canvas.getContext('2d');
var raf;
var running = false;
var mouseOldPos;
var mousePos;
var wallLine = {
draw: function() {
ctx.beginPath();
ctx.moveTo(mouseOldPos.x, mouseOldPos.y);
ctx.lineTo(mousePos.x, mousePos.y);
ctx.stroke();
}
};
function clear() {
ctx.fillStyle = 'rgba(255, 255, 255, 1)';
ctx.fillRect(0,0,canvas.width,canvas.height);
}
canvas.addEventListener('mousemove', function(e) {
ctx.lineWidth = 10.0;
ctx.lineCap = 'square';
if (running) {
clear();
mousePos = getMousePos(canvas, e);
wallLine.draw();
}
});
canvas.addEventListener('click', function(e) {
if (!running) {
mouseOldPos = getMousePos(canvas, e);
running = true;
} else {
running = false;
}
});
function getMousePos(canvas, e) {
var rect = canvas.getBoundingClientRect();
return {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
}
{% extends "base_generic.html" %}
{% block content %}
<h1>Name: {{ plan.name }}</h1>
<strong>Architect:</strong> {{ plan.architect }}</p>
<p><strong>Approved:</strong> {{ plan.approved }}</p>
<p><strong>num_elements:</strong> {{ num_elements }}</p>
<p><strong>Approve Date:</strong> {{ plan.approve_date }}</p>
{% if perms.catalog.can_edit_plan %}
<a href="{% url }">Edit</a>
{% endif %}
<canvas id="canvas" style="border: 1px solid" width="1000" height="700"></canvas>
{% load static %}
<script type="text/javascript" src="{% static 'js/canvas.js' %}"></script>
<script>
var canvas = document.getElementById('canvas');
</script>
{% endblock %}
class Element(models.Model):
"""Element - it's wall or smth else on the axis. Between axises"""
#axis_owner = models.ForeignKey(Axis, on_delete=models.SET_NULL, null=True, blank=True, related_name='axis_owner') # need for slanted axises???
plan_id = models.UUIDField(null=True, blank=True)
axis_id = models.IntegerField()
x0 = models.IntegerField()
y0 = models.IntegerField()
x1 = models.IntegerField()
y1 = models.IntegerField()
# for explicitly specify alignment. By default alignment define automatically:
ALIGN = (
('l', 'Left'),
('r', 'Right'),
('c', 'Center'),
('a', 'Auto'),
)
align = models.CharField(
max_length=1,
choices=ALIGN,
blank=True,
default='c',
help_text='You can explicitly specify alignment option. Default = Auto',
)
def save(self, *args, **kwargs):
super(Element, self).save(*args, **kwargs)
def __str__(self):
"""String for representing the Model object."""
return self.axis_id
path('plan/<uuid:pk>/', views.canvasData, name='plan-detail'),
def canvasData(request, pk):
"""View function for elements rendering on canvas"""
context = {
}
return render(request, 'catalog/plan_detail.html', context=context)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question