Home > Web Front-end > H5 Tutorial > Application of html5-image cropping renderings

Application of html5-image cropping renderings

零下一度
Release: 2017-04-27 15:25:07
Original
1762 people have browsed it

How to crop an image using HTML5? The graphics drawn later will use this cropping area. To cancel this cropping area, you need to use the save and restore state. Here are the screenshots of the

effect. :


#Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>canvas</title>
<script>
    // 图像裁剪:context.clip()

    // context.clip()只绘制封闭路径区域内的图像,不绘制路径外部图像,用的时候

    // 先创建裁剪区域

    // 再绘制图像(之后绘制的图形都会采用这个裁剪区域,要取消这个裁剪区域就需要用到保存恢复状态,下面有讲)

    // 给出圆形和星形的裁剪代码
     function createCircleClip(context) {
        context.beginPath();
        context.arc(200, 170, 100, 0, Math.PI * 2, true);
        context.closePath();
        context.clip();
    }

    function create5StarClip(context) {
        var n = 0;
        var dx = 200;
        var dy = 150;
        var s = 150;
        context.beginPath();
        var x = Math.sin(0);
        var y = Math.cos(0);
        var dig = Math.PI / 5 * 4;
        for (var i = 0; i < 5; i++) {
           var x = Math.sin(i * dig);
           var y = Math.cos(i * dig);
           context.lineTo(dx + x * s, dy + y * s);
        }
        context.closePath();
        context.clip();
    } 

	function draw() {
        var canvas = document.getElementById("mycanvas");
        if (canvas == null)
         return false;

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

        context.fillStyle = "black";
        context.fillRect(0, 0, 400, 300);
        image = new Image();
        image.src = "Image/html5.jpg"

        image.onload = function () {
            //圆形裁剪区
            //createCircleClip(context)
            //星形裁剪区
            create5StarClip(context);
            context.drawImage(image,0,0);
        }
        
	} 		

	window.onload=draw;
</script>
</head>
<body>
  <p id="p1"></p>
  <canvas id="mycanvas" width="1000" height="800">当前浏览器不支持canvas</canvas>
</body>
</html>
Copy after login

The above is the detailed content of Application of html5-image cropping renderings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template