0
我正在創建一個畫布事物來爲我製作的2D遊戲創建簡單的高度圖。在普通的畫布尺寸(150px * 300px)下,它看起來很正常,但如果我想縮放畫布(對於更大的地圖),並將內部內容的大小保持相同的大小,我目前所做的並不起作用。HTML-畫布元素增加尺寸,但保持內容尺寸相同
我的代碼是在這裏:
<body>
<script>
var hai = 1; //World width. I was tinkering with this to make the result that I wanted, but at the end, the entire heigtmap needs to be exported via base64 url. 5 canvas' right next to eachother LOOKS like what I want, but I can't get a base64 url for all 5 together, only one.
var runnin = 0;
while(runnin != hai){
var can = document.createElement("canvas");
can.id = "canvasite"
can.style = "width: 150px; height: 75px;"
document.body.appendChild(can);
c = document.getElementById('canvasite'),
ctx = c.getContext('2d');
var x = 0
if(runnin == 0){
var endheight = c.height/2
}
var y = endheight;
var widd = 0
while(widd != c.width){
ctx.fillStyle = "rgba("+0+","+255+","+0+","+(255/255)+")";
ctx.fillRect(x, y, 1, 1);
var ychan = Math.floor((Math.random() * 6) + 1);
if(ychan == 1){
var y=y+2
}else if(ychan == 2){
var y=y+1
}else if(ychan == 3){
var y=y-1
}else if(ychan == 4){
var y=y-2
}else{
var y=y
}
var hig = y
while(hig != c.height){
ctx.fillStyle = "rgba("+0+","+255+","+0+","+(255/255)+")";
ctx.fillRect(x, hig, 1, 1);
var hig = hig+1
}
var widd = widd+1
var x=x+1
}
var endheight = y
var runnin = runnin+1
document.getElementById('canvasite').setAttribute('id','nAn')
}
</script>
</body>
正如我在這說,它給了我想要的一半。 它確實給出了一個很長的高度圖,但是我無法從中得到一個base64網址。爲了總結這一切,我試過的不允許我有一個完整的base64 url,並且如果世界寬度是3,但是在1 canvas中,我可以得到相同的結果,所以我可以得到一個base64從它的網址?
在此先感謝,Athdot。
編輯:我是一個程序員的基本,如果在我的代碼任何錯誤,請告訴我