开发者社区 问答 正文

web项目里怎么对某图片动态加文字

我想在基于优惠券的背景图动态生成带文字的优惠券,有什么轮子可以使用吗?或者有其他什么思路,比如不在后台在前台处理?

展开
收起
蛮大人123 2016-03-09 17:46:54 2527 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    <script>
    var text="www.html5party.com"
    function change(){
    text=document.getElementById("text").value;
    init();
    }
    function init(){
    watermark('canvas','http://www.html5party.com/wp-content/uploads/2013/04/google-glasses-1.jpg',text);
    }
        function watermark(id,imgsrc,txt) {
            canvas = document.getElementById(id);
            if (canvas.getContext){
                ctx = canvas.getContext('2d');
                var img1 = new Image();
                img1.src=imgsrc;
                img1.onload = function(){
                    var imgWidth=img1.width;
                    var imgHeight=img1.height;
                    canvas.width= imgWidth;
                    canvas.height=imgHeight;
                    ctx.drawImage(img1,0,0);
                    ctx.fillStyle    = 'rgba(0, 0, 0, 0.25)';
                    ctx.font         = '30px sans-serif';
                    ctx.fillText(txt, canvas.width-(txt.length*15),canvas.height-30);
                    ctx.fillStyle    = 'rgba(255, 255, 255, 0.25)';
                    ctx.fillText(txt, canvas.width-(txt.length*15)-2,canvas.height-32)
                }
            }
        }
    </script>
    2019-07-17 18:56:26
    赞同 展开评论