The canvas clipping doesn't work as expected:
<html>
<head>
</head>
<body onload="draw();">
<script type="text/javascript">
function draw() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0,0,200,200);
// ctx.save();
ctx.beginPath();
ctx.moveTo(30, 30);
ctx.lineTo(30, 70);
ctx.lineTo(70, 70);
ctx.lineTo(70, 30);
ctx.lineTo(30, 30);
ctx.clip();
ctx.fillStyle = "green";
ctx.fillRect(0,0,200,200);
// ctx.restore();
}
</script>
Canvas:<br>
<canvas id="canvas" width="200" height="200"></canvas>
</body>
</html>
if 'ctx.save()/restore()' is uncommented the script works fine.
The reason is WCGraphicsPrismContext creates a new ClipLayer when clipping is needed and pushes the current state to the stack. The same machinery is used for context save/restore functionality. When the drawing is finished, the clip layer is not rendered on the 'primary' layer until the restore() is called.
<html>
<head>
</head>
<body onload="draw();">
<script type="text/javascript">
function draw() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0,0,200,200);
// ctx.save();
ctx.beginPath();
ctx.moveTo(30, 30);
ctx.lineTo(30, 70);
ctx.lineTo(70, 70);
ctx.lineTo(70, 30);
ctx.lineTo(30, 30);
ctx.clip();
ctx.fillStyle = "green";
ctx.fillRect(0,0,200,200);
// ctx.restore();
}
</script>
Canvas:<br>
<canvas id="canvas" width="200" height="200"></canvas>
</body>
</html>
if 'ctx.save()/restore()' is uncommented the script works fine.
The reason is WCGraphicsPrismContext creates a new ClipLayer when clipping is needed and pushes the current state to the stack. The same machinery is used for context save/restore functionality. When the drawing is finished, the clip layer is not rendered on the 'primary' layer until the restore() is called.
- relates to
-
JDK-8096612 [WebView] "outstanding resource locks detected" when using clip in canvas
- Resolved