Giter Site home page Giter Site logo

gitjinfeiyang / easy-canvas Goto Github PK

View Code? Open in Web Editor NEW
654.0 654.0 73.0 1.4 MB

使用render函数在canvas中创建文档流布局,小程序海报图、小程序朋友圈分享图。easy-canvas is a powerful tool helps us easy to layout with canvas.

Home Page: https://gitjinfeiyang.github.io/easy-canvas/example

License: MIT License

JavaScript 69.62% HTML 30.38%
canvas css html javascript vue

easy-canvas's Issues

Cannot read property 'getContext' of null

小程序this.createSelectorQuery()有时候会拿不到DOM

有人推荐写在nextTick里,但还是会存在拿不到,有更好的写法吗🐶

wx.nextTick(() => {
  ...
  this.createSelectorQuery()
     .exec(res => {
            const canvas = res[0].node;
      })
  ...
}

安卓和ios图片都没有渲染出来

downFile域名白名单已加,
没有勾选 不校验域名,我自己使用downFile也成功的,应该不是这个问题
但是真机上图片出不来,文字是显示了,排版也正常
模拟器上是可以的
还有什么问题导致?
有个lifecycle事件用处是什么?
可以看下下面的代码哪里有问题

drawPoster() {
    const query = wx.createSelectorQuery()
    query.select('#myCanvas')
      .fields({ node: true, size: true })
      .exec((res) => {
        const canvas = res[0].node
        const ctx = canvas.getContext('2d')

        const dpr = wx.getSystemInfoSync().pixelRatio
        console.log('dpr', dpr)
        canvas.width = res[0].width * dpr
        canvas.height = res[0].height * dpr
        // ctx.scale(dpr, dpr)
        const containerWidth = canvas.width;
        const containerHeight = canvas.height;
        this.canvas = canvas;
        this.layer = ef.createLayer(ctx, {
          dpr,
          width: containerWidth,
          height: containerHeight,
          lifecycle: {
            onEffectSuccess: () => {
              console.log('onEffectSuccess')
            },
            onEffectFail() {
              // 网络请求失败
              showToast('部分图片下载失败')
            },
          }, canvas
        })
        this.createNode(containerWidth, containerHeight)
      })
  },
  createNode(containerWidth, containerHeight) {
    // 商品数据
    const detail = this.data.detail;
    // 小程序路径
    const params = { path: 'pages/index/index', appId: config.appId, scene: `0_${detail.spuId}_${detail.activityId}` };
    // 小程序二维码地址
    const qrurl = `${config.baseUrl2}/api/mp/create-unlimit-qrcode?${app.toQuery(params)}`
    console.log(`qrurl`, qrurl)
    this.setData({
      qrurl
    })
    // logo
    const logoUrl = `https://myunonline-qyyx-1304736005.cos.ap-shanghai.myqcloud.com/mp-customer-images/logo-share.png`;
    // Promise.all([
    //   downloadFile(logoUrl),
    //   downloadFile(detail.sharePicUrl),
    //   downloadFile(qrurl)
    // ]).then(tempFiles => {
    //   console.log('tempFils', tempFiles)
    // }).catch(err => {
    //   console.error('tempFils err', err)
    // })
    const node = ef.createElement(c => {
      return c(
        'view',
        {
          styles: {
            backgroundColor: '#fff',
            width: containerWidth,
            height: containerHeight
          }
        },
        [
          // logo
          c(
            'view',
            {
              styles: {
                backgroundColor: '#fff',
                textAlign: 'center',
                display: 'flex',
                justifyContent: 'center',
                marginTop: 20
              }
            },
            [
              c(
                'image',
                {
                  styles: {
                    width: 68
                  },
                  attrs: {
                    src: logoUrl,
                  },
                }
              )
            ]
          ),
          // 商品图片
          c(
            'view',
            {
              styles: {
                backgroundColor: '#fff',
                textAlign: 'center',
                display: 'flex',
                justifyContent: 'center',
                marginTop: 20
              }
            },
            [
              c(
                'image',
                {
                  styles: {
                    width: containerWidth - 120,
                    height: containerWidth - 120,
                    borderRadius: 25,
                  },
                  attrs: {
                    src: detail.sharePicUrl,
                  },
                }
              )
            ]
          ),
          // 商品标题
          c(
            'view',
            {
              styles: {
                backgroundColor: '#fff',
                marginTop: 20,
                marginLeft: 60,
                marginRight: 60
              }
            },
            [
              c(
                'text',
                {
                  styles: {
                    fontSize: 30,
                    color: '#000',
                    maxLine: 2
                  },
                },
                detail.shareTitle,
              )
            ]
          ),
          // 商品推荐语
          c(
            'view',
            {
              styles: {
                backgroundColor: '#fbfbfb',
                borderRadius: 15,
                borderColor: 'rgba(221, 221, 221, .6)',
                borderWidth: 1,
                marginTop: 20,
                marginLeft: 60,
                marginRight: 60,
                padding: [17, 26, 17, 26]
              }
            },
            [
              c(
                'text',
                {
                  styles: {
                    fontSize: 20,
                    color: '#000',
                    maxLine: 1
                  },
                },
                detail.shareDesc,
              )
            ]
          ),
          // 小程序码
          c(
            'view',
            {
              styles: {
                borderTopWidth: 1,
                display: 'flex',
                borderColor: '#eeeeee',
                marginTop: 20,
                alignItems: 'center',
                padding: [20, 60, 20, 60]
              },
            },
            [
              c('image', {
                styles: {
                  width: 140,
                  height: 140,
                  borderRadius: 140,
                },
                attrs: {
                  src: qrurl,
                },
              }),
              c('text', {
                styles: {
                  flex: 1,
                  paddingLeft: 30,
                  fontSize: 28,
                  color: '#000'
                }
              }, '长按识别查看商品详情')],
          ),
        ],
      );
    });
    node.mount(this.layer)
    console.log(node)
  }

在node环境支持图片加载

Image现在会报未定义,其实node-canvas包提供了Image类,如果可以做成可传入的配置就好了
本地改了下,使用Image类类似WX一样处理就能正常渲染

请问背景色linear-gradient要怎么写才生效

c( 'view', { styles: { backgroundColor: 'linear-gradient(180deg, #FF6A00 0%, #FF9C55 100%)' }, // 样式 attrs: {}, // 属性 比如src on: {}, // 事件 如click load }, [c('text', { color: '#fff' }, 'Hello World')] )
这样写是没有生效的

添加 onload lifecycle

需要提供一个与 document.onload 语义相同的 lifecycle,不然 nodejs 中没有执行 canvas to image 的时机。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.