相关文章推荐
暗恋学妹的开心果  ·  为什么我的Axios取给CORS错误?-腾讯 ...·  1 年前    · 
读研的面包  ·  互联网名称与数字地址分配机构章程 | ...·  1 年前    · 
慷慨的黄瓜  ·  Java单元测试用例的编写,有什么技巧? - 知乎·  1 年前    · 
酷酷的黄花菜  ·  vue使用v-for遍历map,获得对应的 ...·  1 年前    · 
胆小的鼠标  ·  libusb(3)·  1 年前    · 
Code  ›  原 html2canvas解决字体渐变开发者社区
font
https://cloud.tencent.com/developer/article/1145758
力能扛鼎的饭卡
1 年前
作者头像
杭州前端工程师
0 篇文章

原 html2canvas解决字体渐变

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 前端大白专栏 > 原 html2canvas解决字体渐变

原 html2canvas解决字体渐变

作者头像
杭州前端工程师
发布 于 2018-06-08 15:33:25
2.2K 1
发布 于 2018-06-08 15:33:25
举报
  • 前言

这两天有个需求,需要保存页面的dom元素为图片到本地,之前没有做过类似的效果,通过github搜索了一下,找到两个github库,

一个为domtoimage:https://github.com/tsayen/dom-to-image; star:3539;

另一个为htmltocanvas:https://github.com/niklasvh/html2canvas/, star12073;

两个都试用后,最后选择了第二个.

使用方法

使用方法很简单:在正常的html中引入html2canvas.js后只需要写如下几行代码;

html2canvas(document.getElementById("container"))
  .then(function (canvas) {
    document.body.appendChild(canvas)
  })

遇到的问题

在使用文本渐变功能的时候,在进行生成图片的时候渐变的文本不能正常的显示渐变内容

明明canvas是支持文本渐变的,这怎么就实现不了了,是官网查了一下and看了github的issues,确实也有很多同学遇到和我同样的问题:https://github.com/niklasvh/html2canvas/issues/1208;作者也在下面回复了,目前暂时不支持:Neither background-clip: text nor box-shadow are currently supported.

在官网上也看到了相关的说明.

需求方这边又要求必须要这样的渐变的效果

解决方案--改源码

首先就得找到渲染文本的函数:

{
  key: 'renderNodeContent',
  value: function renderNodeContent(container) {
    var _this = this;
    var callback = function callback() {
      if (container.childNodes.length) {
        container.childNodes.forEach(function (child) {
          if (child instanceof _TextContainer2.default) {
            var style = child.parent.style;
            var xnwWidth = 0;
            var xnwLeft = child.bounds[0].bounds.left;
            for (var xnwi = 0; xnwi < child.bounds.length; xnwi++) {
              xnwWidth += child.bounds[xnwi].bounds.width;
            xnwWidth = xnwLeft + xnwWidth;
            _this.target.renderTextNode(child.bounds, style.color, style.font, style.textDecoration, style.textShadow, xnwWidth,xnwLeft);
          } else {
            _this.target.drawShape(child, container.style.color);
},

这段函数是渲染内容页面内容的,这个函数调用了renderTextNode 这个函数,我们继续走,一看这个函数名就是渲染文本的.在这个函数里我定义了一些变量都是带我名字的首字母,为了防止冲突.

继续走:

{
  key: 'renderTextNode',
  value: function renderTextNode(textBounds, color, font, textDecoration, textShadows, xnwWidth,xnwLeft) {
    var _this4 = this;
    console.log(xnwWidth);
    this.ctx.font = [font.fontStyle, font.fontVariant, font.fontWeight, font.fontSize, font.fontFamily].join(' ');
    console.log(this.options);
    textBounds.forEach(function (text) {
      // _this4.ctx.fillStyle = '#ff0000';
      if (textShadows && text.text.trim().length) {
        textShadows.slice(0).reverse().forEach(function (textShadow) {
          _this4.ctx.shadowColor = textShadow.color.toString();
          _this4.ctx.shadowOffsetX = textShadow.offsetX * _this4.options.scale;
          _this4.ctx.shadowOffsetY = textShadow.offsetY * _this4.options.scale;
          _this4.ctx.shadowBlur = textShadow.blur;
          _this4.ctx.fillText(text.text, text.bounds.left, text.bounds.top + text.bounds.height);
      } else {
         // 当fontsize的大小为30.1px的时候,才给它加上渐变的效果
        if (font.fontSize === '30.1px') {
          // xnwLeft为渐变的开始位置,xnwWidth为渐变的长度
          var gradientxnw = _this4.ctx.createLinearGradient(xnwLeft, 0, xnwWidth, 0);
          gradientxnw.addColorStop(0, 'black');
          gradientxnw.addColorStop(0.8, 'red');
          gradientxnw.addColorStop(1, 'blue');
          _this4.ctx.fillStyle = gradientxnw;
        } else {
          _this4.ctx.fillStyle = color;
 
推荐文章
暗恋学妹的开心果  ·  为什么我的Axios取给CORS错误?-腾讯云开发者社区-腾讯云
1 年前
读研的面包  ·  互联网名称与数字地址分配机构章程 | 2011年12月8日修订 | 加利福尼亚州公益性非营利机构 - ICANN
1 年前
慷慨的黄瓜  ·  Java单元测试用例的编写,有什么技巧? - 知乎
1 年前
酷酷的黄花菜  ·  vue使用v-for遍历map,获得对应的 key 和 value_vue遍历map的key__修铁路的的博客-CSDN博客
1 年前
胆小的鼠标  ·  libusb(3)
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号