问题描述:

I need to draw in a canvas an image taken from the database (actually more images, one for canvas). The image depend on the user but once the page is loaded it don't change so I could pass it in the view, if usefull. I don't know how to say to the javascript function which image to draw.

My models.py:

class Forma(models.Model):

name = models.CharField(max_length=64)

percorso = models.ImageField(upload_to='/mypath',

blank=True, default='')

My views.py:

def homepage(request):

...

tipo=user_profile.tipo

forma_list=Forma.objects.filter(tipo=tipo)

context_dict['forma_list']=forma_list

return render(request, 'homepage.html', context_dict)

My homepage.html:

 <script>

<!--

window.onload = function() {

crea_image();

};

-->

</script>

...

<canvas id='canvas1'>

Your browser does not support the HTML5 canvas tag.

</canvas>

My script-jquery.js:

function crea_image() {

// basically I want something like that:

//var forme = context_dict['form_list']

var canvas1 = document.getElementById("canvas1");

var context = canvas1.getContext("2d");

var base = new Image();

base.src = forme[0].percorso

base.onload = function() {

context.drawImage(base, 0, 0);

};

};

I search but I found reference to other file, like that

var xmlhttp = new XMLHttpRequest();

var url = "myTutorials.txt";

or to url like that

xmlhttp.open("GET", url, true);

and I'm not sure if it's good for my situation.

相关阅读:
Top