亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Based on the dismantling of the above requirements
%%PRE_BLOCK_8%%
Home WeChat Applet Mini Program Development Use the small program canvas to write a simple picture application

Use the small program canvas to write a simple picture application

Dec 16, 2020 pm 06:00 PM
canvas front end Applets

Small program development tutorialThe column introduces the use of canvas to write a picture

Use the small program canvas to write a simple picture application

Recommendation (free): Mini program development tutorial

##Application display

Screenshot

Use the small program canvas to write a simple picture application

Requirements

Since it is a small application, I hope that the final product has

applicable scenarios And it is a valuable

source of demand

inspiration for this application requirement


In my previous work life, I often got # from my colleagues inadvertently. ##美photo
At this time we want to make this photo into an emoticon pack

Generally add a few captions to the picture

An interesting communication The tool (emoticon package) has completed the


Requirement analysis

Based on the dismantling of the above requirements

You can sort out the implementation of the application functions


Users need to upload a picture
  • You can add text
  • Text can be
  • style adjusted
  • and rotation and zoom In addition, we hope that we can also insert some stickers
  • The stickers can be rotated and zoomed
  • Users can export pictures to the album
  • implementation

github warehouse

https://github.com/luosijie/f...

If you like my project, please give me a star for encouragement Let’s take a look

This application is developed using mini program

Using framework: mpx

    Using technology: mini program canvas
  • State Management

import?{?createStore?}?from?'@mpxjs/core'

const?store?=?createStore({
??state:?{
????cavas:?null,?????????//?cnavas實(shí)例
????ctx:?null,???????????//?canvas上下文實(shí)例
????elements:?[],????????//?canvas元素
????activeIndex:?null,???//?當(dāng)前編輯中的元素索引
????mode:?'background',??//?當(dāng)前編輯模式:background,?text,?sticker
????fontStyle:?{?????????//?文字默認(rèn)樣式
??????opacity:?1,
??????fillStyle:?'#000000',
??????strokeStyle:?'#000000'
????}
??},
??mutations:?{
????setCanvas?(state,?data)?{
??????state.canvas?=?data
????},
????setCtx?(state,?data)?{
??????state.ctx?=?data
????},
????setElements?(state,?data)?{
??????state.elements?=?data
????},
????setMode?(state,?data)?{
??????state.mode?=?data
????},
????setActiveIndex?(state,?data)?{
??????state.activeIndex?=?data
????},
????setFontStyle?(state,?{?key,?data?})?{
??????state.fontStyle[key]?=?data
????},
????//?添加文字
????addText?(state)?{
??????const?size?=?50
??????const?string?=?'請(qǐng)輸入文字'
??????const?text?=?{
????????type:?'text',
????????data:?string,
????????scale:?1,
????????size,
????????left:?100,
????????top:?100,
????????rotate:?0,
????????opacity:?state.fontStyle.opacity,
????????fillStyle:?state.fontStyle.fillStyle,
????????strokeStyle:?state.fontStyle.strokeStyle
??????}
??????state.elements.push(text)
??????state.activeIndex?=?state.elements.length?-?1
????},
????//?添加貼圖
????addSticker?(state,?data)?{
??????state.elements.push(data)
??????state.activeIndex?=?state.elements.length?-?1
????},
????//?刪除當(dāng)前選中
????deleteActiveELement?(state)?{
??????state.elements.splice(state.activeIndex,?1)
??????state.activeIndex?=?null
????},
????//?清空畫布
????clear?(state)?{
??????state.elements?=?[]
??????state.activeIndex?=?null
????}
??}
})

export?default?store
Canvas Initialization

//?初始化畫布
async?initCanvas()?{
??const?query?=?this.createSelectorQuery()
??query
????.select('#canvas')
????.fields({?node:?true,?size:?true?})
????.exec(async?res?=>?{
??????const?canvas?=?res[0].node
??????const?ctx?=?canvas.getContext('2d')
??????store.commit('setCanvas',?canvas)
??????store.commit('setCtx',?ctx)

??????await?this.loadImage('/images/icon-rotate.png').then(res?=>?{
????????this.image.rotate?=?res
??????})

??????canvas.width?=?res[0].width?*?this.dpr
??????canvas.height?=?res[0].height?*?this.dpr
??????ctx.scale(this.dpr,?this.dpr)
??????this.drawGrid()
????})
}
Draw Picture

/**
?*?繪制圖片
?*?@param?{?Object?}?ele?canvas元素
?*/
drawImage(ele)?{
??this.ctx.save()
??const?width?=?ele.width
??const?height?=?ele.height
??const?centerX?=?ele.left?+?ele.width?/?2
??const?centerY?=?ele.top?+?ele.height?/?2
??this.ctx.translate(centerX,?centerY)
??this.ctx.rotate(ele.rotate)
??this.ctx.drawImage(ele.data,?ele.left?-?centerX,?ele.top?-?centerY,?width,?height)
??this.ctx.restore()
}
Draw Text

/**
?*?繪制文字
?*?@param?{?Object?}?ele?canvas元素
?*/
drawText(ele)?{
??this.ctx.save()
??const?width?=?ele.size?*?ele.data.length
??const?height?=?ele.size
??const?centerX?=?ele.left?+?width?/?2
??const?centerY?=?ele.top?+?height?/?2
??this.ctx.translate(centerX,?centerY)
??this.ctx.rotate(ele.rotate)
??this.ctx.font?=?`${ele.size}px?bold?sans-serif`
??this.ctx.globalAlpha?=?ele.opacity
??this.ctx.fillStyle?=?ele.fillStyle
??this.ctx.strokeStyle?=?ele.strokeStyle
??//?this.ctx.lineWidth?=?2
??this.ctx.textBaseline?=?'top'
??console.log('draw-text',?ele)
??this.ctx.fillText(ele.data,?ele.left?-?centerX,?ele.top?-?centerY)
??this.ctx.strokeText(ele.data,?ele.left?-?centerX,?ele.top?-?centerY)
??this.ctx.restore()
}
Drawing control element

initController(ele)?{
??const?cs?=?this.convert2ControllerSize(ele)
??this.ctx.save()
??this.ctx.strokeStyle?=?'#eee'
??this.ctx.translate(cs.centerX,?cs.centerY)
??this.ctx.rotate(cs.rotate)
??//?繪制虛線邊框
??this.ctx.setLineDash([10,?5],?5)
??this.ctx.strokeRect(cs.left?-?cs.centerX,?cs.top?-?cs.centerY,?cs.width,?cs.height)
??//?繪制控制點(diǎn)-旋轉(zhuǎn)
??this.ctx.drawImage(this.image.rotate,?cs.left?+?cs.width?-?10?-?cs.centerX,?cs.top?+?cs.height?-?10?-?cs.centerY,?20,?20)
??this.ctx.restore()
}
Canvas rendering function

//?畫布渲染函數(shù)
renderCanvas()?{
??this.ctx.clearRect(0,?0,?this.ctx.canvas.width,?this.ctx.canvas.height)
??this.drawGrid()
??console.log('draw-background',?this.background)
??if?(this.background)?this.drawImage(this.background)
??for?(let?i?=?0;?i?<strong></strong>Event listening<p><strong></strong>Move</p><p></p><pre class="brush:php;toolbar:false">//?移動(dòng)事件綁定函數(shù)
handleMove(e)?{
??console.log('mouse-move',?e)
??if?(e.touches.length?>?1)?return
??const?x?=?e.touches[0].x
??const?y?=?e.touches[0].y
??const?dx?=?this.startTouches[0].x?-?x
??const?dy?=?this.startTouches[0].y?-?y
??const?elements?=?this.elements.slice()
??elements[this.activeIndex?||?0].left?=?this.startSelected.left?-?dx
??elements[this.activeIndex?||?0].top?=?this.startSelected.top?-?dy
??store.commit('setElements',?elements)
}
Rotate
//?旋轉(zhuǎn)綁定函數(shù)
handleRotate(e)?{
??console.log('handleRotate')
??const?start?=?this.startTouches[0]
??const?end?=?e.touches[0]
??const?center?=?{
????x:?this.startSelected.centerX,
????y:?this.startSelected.centerY
??}
??const?startLength?=?Math.sqrt((center.x?-?start.x)?**?2?+?(center.y?-?start.y)?**?2)
??const?endLength?=?Math.sqrt((center.x?-?end.x)?**?2?+?(center.y?-?end.y)?**?2)
??const?radian?=?this.convert2Radian(start,?end,?center)
??const?scale?=?endLength?/?startLength
??const?elements?=?this.elements.slice()
??const?selected?=?elements[this.activeIndex]
??//?旋轉(zhuǎn)
??selected.rotate?=?this.startSelected.rotate?-?radian
??//?縮放
??if?(selected.type?===?'text')?{
????selected.left?=?this.startSelected.centerX?-?this.startSelected.size?*?this.startSelected.data.length?*?scale?/?2
????selected.top?=?this.startSelected.centerY?-?this.startSelected.size?*?scale?/?2
????selected.size?=?this.startSelected.size?*?scale
??}
??if?(selected.type?===?'sticker')?{
????selected.left?=?this.startSelected.centerX?-?this.startSelected.width?*?scale?/?2
????selected.top?=?this.startSelected.centerY?-?this.startSelected.height?*?scale?/?2
????selected.width?=?this.startSelected.width?*?scale
????selected.height?=?this.startSelected.height?*?scale
??}
??store.commit('setElements',?elements)
}
Zoom

//?縮放事件綁定函數(shù)
handleScale(e)?{
??if?(e.touches.length?!==?2?||?this.mode?!==?'background')?return
??const?startLength?=?Math.sqrt(
????(this.startTouches[0].x?-?this.startTouches[1].x)?**?2?+
??????(this.startTouches[0].y?-?this.startTouches[1].y)?**?2
??)
??const?endLength?=?Math.sqrt(
????(e.touches[0].x?-?e.touches[1].x)?**?2?+?(e.touches[0].y?-?e.touches[1].y)?**?2
??)
??const?scale?=?endLength?/?startLength
??const?elements?=?this.elements.slice()
??const?selected?=?elements[this.activeIndex?||?0]
??selected.left?=?this.startSelected.centerX?-?this.startSelected.width?*?scale?/?2
??selected.top?=?this.startSelected.centerY?-?this.startSelected.height?*?scale?/?2
??selected.width?=?this.startSelected.width?*?scale
??selected.height?=?this.startSelected.height?*?scale
??//?elements[this.activeIndex?||?0].scale?=?this.startSelected.scale?*?scale
??store.commit('setElements',?elements)
}

Export image

export()?{
??if?(!store.state.elements.length)?{
????wx.showToast({
??????title:?'加點(diǎn)東西再導(dǎo)出吧',
??????icon:?'none'
????})
????return
??}
??wx.showModal({
????title:?'提示',
????content:?'圖片將保存到手機(jī)相冊(cè)',
????success(res)?{
??????if?(res.confirm)?{
????????console.log('export-canvas',?store.state.ctx)
????????const?canvas?=?store.state.canvas
????????wx.canvasToTempFilePath({
??????????x:?0,
??????????y:?0,
??????????width:?canvas.width,
??????????height:?canvas.height,
??????????canvas,
??????????complete(res)?{
????????????if?(res.errMsg?===?'canvasToTempFilePath:ok')?{
??????????????wx.saveImageToPhotosAlbum({
????????????????filePath:?res.tempFilePath,
????????????????success(res)?{
??????????????????wx.showToast({
????????????????????title:?'圖片保存成功',
????????????????????icon:?'none'
??????????????????})
????????????????}
??????????????})
????????????}
??????????}
????????})
??????}
????}
??})
}

The above is the detailed content of Use the small program canvas to write a simple picture application. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

Exploring Go language front-end technology: a new vision for front-end development Exploring Go language front-end technology: a new vision for front-end development Mar 28, 2024 pm 01:06 PM

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces

Is Django front-end or back-end? check it out! Is Django front-end or back-end? check it out! Jan 19, 2024 am 08:37 AM

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.

Learn the canvas framework and explain the commonly used canvas framework in detail Learn the canvas framework and explain the commonly used canvas framework in detail Jan 17, 2024 am 11:03 AM

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

Explore the powerful role and application of canvas in game development Explore the powerful role and application of canvas in game development Jan 17, 2024 am 11:00 AM

Understand the power and application of canvas in game development Overview: With the rapid development of Internet technology, web games are becoming more and more popular among players. As an important part of web game development, canvas technology has gradually emerged in game development, showing its powerful power and application. This article will introduce the potential of canvas in game development and demonstrate its application through specific code examples. 1. Introduction to canvas technology Canvas is a new element in HTML5, which allows us to use

Django: A magical framework that can handle both front-end and back-end development! Django: A magical framework that can handle both front-end and back-end development! Jan 19, 2024 am 08:52 AM

Django: A magical framework that can handle both front-end and back-end development! Django is an efficient and scalable web application framework. It is able to support multiple web development models, including MVC and MTV, and can easily develop high-quality web applications. Django not only supports back-end development, but can also quickly build front-end interfaces and achieve flexible view display through template language. Django combines front-end development and back-end development into a seamless integration, so developers don’t have to specialize in learning

Combination of Golang and front-end technology: explore how Golang plays a role in the front-end field Combination of Golang and front-end technology: explore how Golang plays a role in the front-end field Mar 19, 2024 pm 06:15 PM

Combination of Golang and front-end technology: To explore how Golang plays a role in the front-end field, specific code examples are needed. With the rapid development of the Internet and mobile applications, front-end technology has become increasingly important. In this field, Golang, as a powerful back-end programming language, can also play an important role. This article will explore how Golang is combined with front-end technology and demonstrate its potential in the front-end field through specific code examples. The role of Golang in the front-end field is as an efficient, concise and easy-to-learn

See all articles