SVG世界最受欢迎矢量图像格式设计师、开发师和内容创建者使用超轻可缩也可以编辑为代码,允许定制提高SEO设计无障碍性
sVG从零开始创建可能比较复杂,但在此教程中,你将学习从任何图像生成sVG图像和代码
执行云情向量化
效果创建SVG图像并使用NuxtAPI上传、获取并转换SVG图像编码
View全模教程开码箱:
源码上也可用GitHub.
要理解此教程中的概念,你应该有以下内容:
先打开它starter模板URL浏览器生成Github重写基于 starter模板
来,你设置nuxt-imgtosvg-demo
名回购
计算机上打开终端窗口并浏览首选文件夹,然后克隆新创建并命令如下:
git克隆<your-github-repo>
代码语言htmlXML高山市xml)
成功克隆到您的个人电脑后,浏览文件夹并安装依存线程
:
igtosvg-de
或使用微信
:
dnuxt-imgtosvg-demonpm安装
成功安装依赖关系后, 打开首选代码编辑器中的项目并运行终端下方代码启动开发服务器
yerndev
或使用微信
:
m运行dev
命令将服务项目并加热重载局部host3:
浏览器视图
获取云名
新建浏览器标签打开云化仪表板并拷贝您的Cloud名 :
根目录项目创建.env
文件并更新代码如下:
CLOUDINARY_CLOUDNAME=your-cloudinary-cloud-name
启动客户端上传
启动客户端上传云端需要非签名上传预置浏览器中浏览云端设置并点击上传标签并滚动到上传预设
点击添加上传预设,指定新预设名称,并修改签名模式为“无签名”,并保存预设
记下上传预设名
云化包安装Nuxt.js
运行下文代码安装Nuxt.js云式包
线程加法@nuxtjs/古典
代码语言CSS系统高山市sss)
或使用微信
:
微信安装@nuxtjs/古典
代码语言CSS系统高山市sss)
配置云化Nuxt.js
成功安装后 打开unxt.config.js
文件并更新代码如下:
导出默认{{//不删除原存代码模块化:[s/axios,'@nuxtjs/cloudinary'万事通云化:{云化Name进程.env.cloudi//不删除原存代码};
代码语言JavaScript高山市javascript)
上方代码中你做了下列工作:
- 添加
@nuxtjs/cloudinary
uxt.js模块数组 - 指定云
云化Name
云形对象
从设备中选择图像
代码编辑器打开页/index.vue
并观察代码线16至26
<输入类型=文件类接受=图像/*名称=文件输入类=隐蔽性@变换=handle更改参考文献=文件输入/>
<按钮时钟点击=handleRef紫色=真实性>选择图像
按钮>
代码语言htmlXML高山市xml)
上方代码
- 上头
接受图像文件并触发
曲柄变换
方法论 - 上头
类表示隐藏
隐藏取自查看器
f=file输入
允许另一个元素引用 - 上头
更新句柄Ref方法滚动到您的 tag and update the
handleRef()
method with the code below:
<script>
// do not remove pre-existing code
export default {
methods: {
handleRef() {
this.$refs.fileInput.click();
},
},
};
script>
Code language: HTML, XML (xml)
In the code above, you did the following:
- Checked for a
fileInput
ref in your project’s list of$refs
- Listened for the
click()
method triggered byfileInput
When a user clicks on the component containing this
handleRef()
method, it triggers a click()
method on the and allows the user to select an image from their device.
Update the handleChange method
In your tag, update the
handleChange()
method with the code below:
<script>
// do not remove pre-existing code
export default {
methods: {
handleChange(e) {
// console.log(e)
const reader = new FileReader();
reader.readAsDataURL(e.target.files[0]);
reader.onload = (onloadEvent) => {
this.setImage = onloadEvent.target.result;
};
},
},
};
script>
Code language: HTML, XML (xml)
In this code, you did the following:
- Created a
reader
variable that contains an instance of JavaScript’sFileReader()
API - The
reader
reads the image file and returns its data as a base64 encoded string, which is passed tosetImage
Due to :src="setImage"
in line 11 of your pages/index.vue
file, will display the selected image on the browser, ready for upload:
In the tag of your
pages/index.vue
, update the handleUpload()
method with the code below:
<script>
// do not remove pre-existing code
export default {
methods: {
async handleUpload() {
this.setUploadStatus = true;
if (this.setImage !== '') {
// Upload image to Cloudinary
const uploadImage = await this.$cloudinary
.upload(this.setImage, {
upload_preset: 'imgtosvg',
})
.then((res) => res.public_id);
} else {
alert('Choose image first!');
}
},
},
};
script>
Code language: HTML, XML (xml)
In the code above, you did the following:
- Set the
setUploadStatus
state totrue
, which triggers the loading state of thecomponent on lines 28 to 34. Note that this
contains an
onClick
prop to trigger thehandleUpload()
method - Checked if the user selected an image
- Created a
uploadImage
variable that uploads your selected image to Cloudinary, withupload_preset
as its parameter. Replace the value ofupload_preset
with yours from Cloudinary - The
upload
method returns an Asset object containing the details of the image you just uploaded. You then returned itspublic_id
, setting it as the value foruploadImage
Generate SVG URL with public_id
Update the handleUpload()
method with the code below:
<script>
// do not remove pre-existing code
export default {
methods: {
async handleUpload() {
this.setUploadStatus = true
if (this.setImage !== '') {
// Upload image to Cloudinary
const uploadImage = await this.$cloudinary
.upload(this.setImage, {
upload_preset: 'your_cloudinary_unsigned_upload_preset_name'
})
.then((res) => res.public_id)
// Convert to SVG
const url = await this.$cloudinary.image.url(uploadImage, {
format: 'svg',
effect: 'vectorize',
})
this.setImage = url
},
}
}
script>
Code language: HTML, XML (xml)
In the code above, you did the following:
- Generated an image URL from
Cloudinary and set its
format
tosvg
and itseffect
tovectorize
- Updated the value of
setImage
tourl
, which will display your converted image in your browser
Extract SVG code from URL
In your terminal, run the command below to install html-formatter
:
yarn add html-formatter
Or using npm
:
npm install html-formatter
This package lets you display auto-formatted HTML code, making it more readable. In your pages/index.vue
, import html-formatter
and update the handleUpload()
method with the code below:
<script>
import formatter from 'html-formatter';
export default {
methods: {
async handleUpload() {
this.setUploadStatus = true;
if (this.setImage !== '') {
// Upload image to Cloudinary
const uploadImage = await this.$cloudinary
.upload(this.setImage, {
upload_preset: 'your_cloudinary_unsigned_upload_preset_name',
})
.then((res) => res.public_id);
// Convert to SVG
const url = await this.$cloudinary.image.url(uploadImage, {
format: 'svg',
effect: 'vectorize',
});
this.setImage = url;
// Extract SVG code
await this.$axios
.$get(url)
.then((data) => (this.svgDisplay = formatter.render(data)));
} else {
alert('Choose image first!');
}
this.setUploadStatus = false;
},
},
};
script>
Code language: HTML, XML (xml)
In the code above, you did the following:
- Imported
html-formatter
asformatter
- Fetched the SVG image from its URL with
$axios
, which returns the SVG code asdata
- Updated the value of
svgDisplay
to the formatteddata
, displaying it on the browser - Disabled the loading state by changing
setUploadStatus
tofalse
Here is the result of your code:
Copy SVG code to Clipboard
In your terminal, run the command below to install copy-to-clipboard
:
yarn add copy-to-clipboard
Or using npm
:
npm install copy-to-clipboard
This package lets you save content to your browser’s clipboard.
In the tag of your
pages/index.vue
, import copy-to-clipboard
and update the copySvg()
method with the code below:
<script>
// do not remove pre-existing code
import copy from 'copy-to-clipboard';
export default {
methods: {
copySvg() {
copy(this.svgDisplay);
this.clipboardStatus = 'Copied!';
setTimeout(() => {
this.clipboardStatus = 'Copy to Clipboard';
}, 2000);
},
},
};
script>
Code language: HTML, XML (xml)
In the code above, you did the following:
- Imported the
copy-to-clipboard
package ascopy
- Saved the current value of
svgDisplay
to your browser’s clipboard withcopy()
- Changed the text of the clipboard button in line 45 to
'Copied!'
, and back to'Copy to Clipboard'
two seconds later
In this article, you learned how to convert an image to SVG using Cloudinary, extract its SVG code and display it in a Nuxt.js project using the tag. Check out these resources below for a deeper understanding of how Nuxt.js or Cloudinary works.