我正在使用 HTML5,我正在尝试使用文本在图像上绘制水印。我有以下代码:
<html>
<body onload="addWaterMarkOnPage();">
<div id="pageContainer" style="width: 612px; height: 792px;">
<canvas id="page1" width="612" height="792"></canvas> //image loading canvas
</canvas>
</div>
</body>
<html>
JavaScript
function addWaterMarkOnPage()
{
var watermarkCanvas=document.createElement("canvas");
watermarkCanvas.id="watermark";
var parentDivElement=document.getElementById("pageContainer")
var parentCanvasElment=document.getElementById("page");
watermarkCanvas.width=parentCanvasElment.width;
watermarkCanvas.height=parentCanvasElment.height;
watermarkCanvas.setAttribute("style","position:absolute")
if(parentDivElement.firstChild)
parentDivElement.insertBefore(watermarkCanvas, parentDivElement.firstChild);
else
parentDivElement.appendChild(watermarkCanvas);
var watermarkContext=watermarkCanvas.getContext('2d');
watermarkContext.globalAlpha=0.5;
watermarkContext.beginPath();
var metrics = watermarkContext.measureText("WaterMark Demo");
var width = metrics.width;
watermarkContext.rotate( (Math.PI / 180) * -45); //rotate 25 degrees
watermarkContext.font = "72px comic Sans MS" ;
watermarkContext.fillStyle = "Red";
watermarkContext.fillText("WaterMark Demo",-width*2,72);
watermarkContext.fill();
}
我想在页面中对角绘制文本(“WaterMark Demo”)。我尝试过多种方式,但没有成功。