欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js canvas實現(xiàn)圓角圖片

 更新時間:2021年09月01日 11:59:20   作者:萬劍  
這篇文章主要為大家詳細介紹了js canvas實現(xiàn)圓角圖片效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了js canvas實現(xiàn)圓角圖片的具體代碼,供大家參考,具體內(nèi)容如下

圓角圖片的代碼實現(xiàn):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="background: rgba(199,237,204,1)">
 
<div style="display:flex; flex-direction: row">
 
    <!--通過style方式為canvas設置寬高在火狐瀏覽器上導致繪制內(nèi)容縱向拉伸。。。-->
    <canvas id="drawing" width="400px" height="400px">canvas to draw</canvas>
 
    <pre id="container" style="margin: 10px"/>
 
    <img src=//img.jbzj.com/file_images/article/202109/202191115608734.jpg>
</div>
</body>
<script type="text/javascript">
 
    window.οnlοad=function () {
        var drawing = document.getElementById('drawing');
 
        if (drawing.getContext) {
            print('support')
            addRoundRectFunc();
            var context = drawing.getContext('2d');
            draw(context);
 
        } else {
            print('not ')
        }
    }
 
 
    function draw(context) {
        context.fillStyle = 'red';
 
        var image = document.images[0];
 
        context.roundRect(0,0,image.width/2,image.height/2,30,true)
 
        context.globalCompositeOperation='source-in';
        context.drawImage(image, 0, 0, image.width / 2, image.height / 2)
        // toImage();
 
    }
 
    function addRoundRectFunc() {
        CanvasRenderingContext2D.prototype.roundRect =
            function (x, y, width, height, radius, fill, stroke) {
                if (typeof stroke == "undefined") {
                    stroke = true;
                }
                if (typeof radius === "undefined") {
                    radius = 5;
                }
                this.beginPath();
                this.moveTo(x + radius, y);
                this.lineTo(x + width - radius, y);
                this.quadraticCurveTo(x + width, y, x + width, y + radius);
                this.lineTo(x + width, y + height - radius);
                this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
                this.lineTo(x + radius, y + height);
                this.quadraticCurveTo(x, y + height, x, y + height - radius);
                this.lineTo(x, y + radius);
                this.quadraticCurveTo(x, y, x + radius, y);
                this.closePath();
                if (stroke) {
                    this.stroke();
                }
                if (fill) {
                    this.fill();
                }
            };
    }
 
    function toImage() {
        var imageUri = drawing.toDataURL('image/png');
        var imageTag = document.createElement('img');
        imageTag.style = 'margin:10px;width:200px;height:200px'
        imageTag.src = imageUri;
        document.body.appendChild(imageTag)
    }
 
    function print(txt) {
        document.getElementById("container").innerHTML += ('\n') + txt;
    }
 
    document.body.onclick = function () {
        window.location.reload()
    }
    console.log = print;
 
 
</script>
 
 
</html>

效果圖:

補充一段使用小代碼:canvas 生成圓角圖片(頭像等)

circleImg(ctx, img, x, y, r) {
    ctx.save();
    var d =2 * r;
    var cx = x + r;
    var cy = y + r;
    ctx.arc(cx, cy, r, 0, 2 * Math.PI);
    ctx.clip();
    ctx.drawImage(img, x, y, d, d);
    ctx.restore();
  }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 一篇文章帶你從零快速上手Rollup

    一篇文章帶你從零快速上手Rollup

    這篇文章主要給大家介紹了如何通過一篇文章快速從零快速上手Rollup的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • NodeJS 模塊開發(fā)及發(fā)布詳解分享

    NodeJS 模塊開發(fā)及發(fā)布詳解分享

    NodeJS 是一門年輕的語言,擴展模塊并不太全,經(jīng)常我們想用某個模塊但是卻找不到合適的
    2012-03-03
  • 微信小程序如何根據(jù)不同用戶切換不同TabBar(簡單易懂!)

    微信小程序如何根據(jù)不同用戶切換不同TabBar(簡單易懂!)

    小程序中我們可能需要根據(jù)不同的權限展示不同的tabbar,下面這篇文章主要給大家介紹了關于微信小程序如何根據(jù)不同用戶切換不同TabBar的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • 不同瀏覽器的怪癖小結(jié)

    不同瀏覽器的怪癖小結(jié)

    收集不同瀏覽器的怪癖,對于大家以后的開發(fā)與兼容性問題,有所幫助。
    2010-07-07
  • JS把字符串轉(zhuǎn)成json對象的三種方法示例詳解

    JS把字符串轉(zhuǎn)成json對象的三種方法示例詳解

    這篇文章主要介紹了js?把字符串轉(zhuǎn)成json對象的三種方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • JS簡單表單驗證功能完整示例

    JS簡單表單驗證功能完整示例

    這篇文章主要介紹了JS簡單表單驗證功能,結(jié)合完整實例形式分析了JavaScript表單驗證相關的字符串判斷、正則驗證、計算等相關操作技巧,需要的朋友可以參考下
    2020-01-01
  • js實現(xiàn)日歷的簡單算法

    js實現(xiàn)日歷的簡單算法

    這篇文章主要為大家詳細介紹了js實現(xiàn)日歷的簡單算法,用js寫了一套日歷,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Nuxt配置Element-UI按需引入的操作方法

    Nuxt配置Element-UI按需引入的操作方法

    這篇文章主要介紹了Nuxt配置Element-UI按需引入方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 通過location.replace禁止瀏覽器后退防止重復提交

    通過location.replace禁止瀏覽器后退防止重復提交

    如果用戶重復提交事件,然后又后退,這樣可能會對某些數(shù)據(jù)產(chǎn)生災難性的問題。所以今天就向大家介紹一種通過location.replace禁止瀏覽器后退按鈕的方法
    2014-09-09
  • javascript實現(xiàn)unicode與ASCII相互轉(zhuǎn)換的方法

    javascript實現(xiàn)unicode與ASCII相互轉(zhuǎn)換的方法

    這篇文章主要介紹了javascript實現(xiàn)unicode與ASCII相互轉(zhuǎn)換的方法,涉及JavaScript字符串的遍歷、正則匹配及編碼轉(zhuǎn)換相關技巧,需要的朋友可以參考下
    2015-12-12

最新評論