我有下面的腳本,需要一個圖像,並繪製一個基於圖像背景顏色的畫布(該圖像使用PHP從數據庫加載),我要求它是它在移動Safari瀏覽器上工作,但不在Chrome上:畫布不畫在手機鉻,但會畫在Safari上
在某些分辨率下,畫布將從波形回退到正方形,並且每當窗口寬度發生變化(移動設備旋轉時增加此寬度)時也調整大小。
我必須注意,這個腳本適用於頁面上的移動設備上,我沒有從圖像中獲取顏色,並且我靜態設置該顏色,導致我認爲這是由於圖像尚未加載。
<script>
function drawCatWave() {
window.onload = function() {
var catImg = new Image();
catImg.src = "/product_images/<?php echo $row_category['logo_colour']; ?>";
var context;
context = document.getElementById('cat-image').getContext('2d');
context.drawImage(catImg, 20, 20, 130, 100);
pixData = (context.getImageData(20, 20, 1, 1).data);
rgb1 = pixData[0].toString();
rgb2 = pixData[1].toString();
rgb3 = pixData[2].toString();
rgb4 = pixData[3].toString();
var rgbString = rgb1 + "," + rgb2 + "," + rgb3 + "," + rgb4;
var rgb = "rgba(" + rgbString + ")";
$('.cat-prod-box h2').css("color", rgb);
console.log(rgb1, rgb2, rgb3);
canvas = document.getElementById("canvas");
var parent = document.getElementById("cat-banner-wave");
ctx = canvas.getContext("2d")
canvas.width = parent.offsetWidth;
canvas.height = parent.offsetHeight;
var actWidth = canvas.width;
var actHeight = canvas.height;
canvas.height = 380;
ctx.lineWidth = 6;
ctx.strokeStyle = "rgba(" + rgbString + ")";
ctx.beginPath();
ctx.moveTo(0, 70);
ctx.bezierCurveTo((actWidth/2), -200, (actWidth/3), 445, actWidth, 100);
ctx.bezierCurveTo(actWidth, 333, actWidth, 333, actWidth, 333);
ctx.bezierCurveTo(0, 333, 0, 333, 0, 333);
ctx.closePath();
ctx.fillStyle = rgb;
ctx.fill();
ctx.stroke();
};
}
function drawCatSquare() {
window.onload = function() {
var catImg = new Image();
catImg.src = "/product_images/<?php echo $row_category['logo_colour']; ?>";
var context;
context = document.getElementById('cat-image').getContext('2d');
context.drawImage(catImg, 20, 20, 130, 100);
pixData = (context.getImageData(20, 20, 1, 1).data);
rgb1 = pixData[0].toString();
rgb2 = pixData[1].toString();
rgb3 = pixData[2].toString();
rgb4 = pixData[3].toString();
var rgbString = rgb1 + "," + rgb2 + "," + rgb3 + "," + rgb4;
var rgb = "rgba(" + rgbString + ")";
$('.cat-prod-box h2').css("color", rgb);
console.log(rgb1, rgb2, rgb3);
canvas = document.getElementById("canvas");
var parent = document.getElementById("cat-banner-wave");
ctx = canvas.getContext("2d")
canvas.width = parent.offsetWidth;
canvas.height = parent.offsetHeight;
var actWidth = canvas.width;
var actHeight = canvas.height;
canvas.height = 380;
ctx.lineWidth = 6;
ctx.strokeStyle = "rgba(" + rgbString + ")";
ctx.rect(0, 0, actWidth, actHeight);
ctx.fillStyle = rgb;
ctx.fill();
ctx.stroke();
};
}
$(document).ready(function() {
if($(this).width() > 500){
drawCatWave();
}else{
drawCatSquare();
}
});
$(window).resize(function() {
if ($(this).width() > 500) {
drawCatWave();
} else {
drawCatSquare();
}
});
</script>
即與畫布不拉絲,由於沒有被加載尚未 – GameAlchemist
嗯形象的問題,我認爲window.load是確保畫布不畫,直到整個窗口就裝,不移動鉻不同的方式處理這個因爲某些原因? – dyatesupnorth
是窗口被裝上window.load。然後,腳本執行,並要求用的.src分配圖像負載。所以結果將取決於緩存在瀏覽器上的處理方式。 (並試試!) – GameAlchemist