记录一下ionic canvas图片

import { Component, Inject, forwardRef } from ‘@angular/core‘;

import { IonicPage, NavController, NavParams } from ‘ionic-angular‘;

import { Screenshot } from ‘@ionic-native/screenshot‘;

import { GlobalFunction } from‘../../services/GlobalFuntion‘;

import { StorageService } from‘../../services/StorageService‘;

import { Global } from ‘../../services/Global‘;

/**

* Generated class for the SharingPage page.

*

* See http://ionicframework.com/docs/components/#navigation for more info

* on Ionic pages and navigation.

*/

@IonicPage()

@Component({

selector: ‘page-sharing‘,

templateUrl: ‘sharing.html‘,

})

export class SharingPage {

public ssssss;

constructor(public navCtrl: NavController,

public navParams: NavParams,

public screenshot: Screenshot,

@Inject (forwardRef (() => GlobalFunction)) private globalFunction: GlobalFunction,

@Inject (forwardRef (() => StorageService)) private storageService: StorageService) {

}

ionViewDidLoad() {

console.log(‘ionViewDidLoad SharingPage‘);

setTimeout(this.attendanceClick(),5000);

}

//测试绘图

attendanceClick() {

let base64Image = ‘assets/images/sharing.png‘;

//加水印

var canvas = document.createElement(‘canvas‘);

var cxt = canvas.getContext(‘2d‘);

cxt.fillStyle = ‘green‘;

cxt.fillRect(10, 10, 100, 100);

var img = new Image();

img.src = base64Image;

img.onload = () => {

var date: string = new Date().toLocaleDateString();

var datetime: string = date;//添加日期

canvas.height = img.height;

canvas.width = img.width;

cxt.drawImage(img,0,0,img.width,img.height,0,0,img.width,img.height);

cxt.save();

cxt.font = 20 + "px Arial";

cxt.textBaseline = ‘middle‘;//更改字号后,必须重置对齐方式,否则居中麻烦。设置文本的垂直对齐方式

cxt.textAlign = ‘center‘;

let ftop = 715;

let fleft = 630;

cxt.fillStyle="#000";

cxt.fillText(datetime,fleft,ftop);//文本元素在画布居中方式

let tempImage = new Image();

let tempImageSrc = this.storageService.getValue(Global.HEAD_IMG_URL);

tempImage.src = ‘assets/images/sharing.png‘;//头像

tempImage.onload = () => {

let tempImageX = 180;

let tempImageY = 310;

let tempImageW = 140;

let tempImageH = 140;

cxt.drawImage(tempImage,tempImageX,tempImageY,tempImageW,tempImageH);

let tempTextData = ‘李二狗‘;

cxt.save();

cxt.font = 40 + "px Arial";

cxt.textBaseline = ‘middle‘;

cxt.textAlign = ‘left‘;

let tempNameX = 360;

let tempNameY = 350;

cxt.fillStyle="#000";

cxt.fillText(tempTextData,tempNameX,tempNameY);

let tempCompanyData = ‘平安人寿‘;

cxt.save();

cxt.font = 40 + "px Arial";

cxt.textBaseline = ‘middle‘;

cxt.textAlign = ‘left‘;

let tempCompanyX = 360;

let tempCompanyY = 420;

cxt.fillText(tempCompanyData,tempCompanyX,tempCompanyY);

this.ssssss= canvas.toDataURL("image/jpg");

console.log(this.ssssss);

}

}

}

//测试截图

public testScreenshots() {

this.screenshot.save(‘jpg‘, 20, ‘myscreenshot.jpg‘).then((res)=>{

this.globalFunction.showAlert(‘save成功‘+res.filePath);

},(err)=>{

this.globalFunction.showAlert(‘save失败‘+err);

});

}

}

按自己实际需求写的

这里来的的参考

参考链接http://www.cnblogs.com/huihuihui/p/6930434.html

时间: 2024-11-03 07:09:33

记录一下ionic canvas图片的相关文章

canvas图片base64编码后,ajax提交到后台

如题,直接上JS var imgData = canvas.toDataURL("image/png"); var data = imgData.substr(22); $.post('Gen', { 'data': data, 'remark': $('remark').val() } ,function(success) { $('#img1').src('data:image/jpeg;base64,' + success); } ); 后台C# string inputStr

h5 canvas 图片上传操作

最近写的小 demo,使用的是h5的 canvas来对图片进行放大,移动,剪裁等等这是最原始的代码,比较接近我的思路,后续会再对格式和结构进行优化 html: 1 <pre name="code" class="brush: html;" rows="15" cols="300"> 2 <input type="file" name="" accept="im

HTML5 Canvas ( 图片填充样式 ) fillStyle, createPattern

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

HTML5 Canvas ( 图片绘制 转化为base64 )

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

【笔记】canvas图片预加载及进度条的实现

/*star *loading模块 *实现图片的预加载,并显示进度条 *参数:图片数组对象,加载完成的回调函数 */ function loadImages(sources,callback){ var loadedImages = 0; var numImages = 0; ctx.font='14px bold'; ctx.lineWidth=5; var clearWidth=canvas.width; var clearHeight=canvas.height; // get num o

Canvas 图片平铺设置

/** * 图片平铺 */ function initDemo7(){ var canvas = document.getElementById("demo7"); if (!canvas) return; var context = canvas.getContext("2d"); var type = [ "no-repeat", // 不平铺 "repeat-x", // 横向平铺 "repeat-y"

HTML5 canvas图片爆炸特效

这是一款基于html5 canvas的炫酷图片爆炸飞散特效js插件.该js插件当用鼠标点击图片时,图片会有玻璃窗被子弹击碎时的爆炸飞散效果,非常炫酷. 在线演示:http://www.htmleaf.com/Demo/201502151384.html 下载地址:http://www.htmleaf.com/html5/html5-canvas/201502151383.html

webrtc学习———记录二:canvas学习

参考资料: http://bucephalus.org/text/CanvasHandbook/CanvasHandbook.html#getcontext2d https://developer.mozilla.org/zh-CN/docs/Web/HTML/Canvas http://www.w3school.com.cn/html5/html5_canvas.asp https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLCanvasEle

canvas图片跨域问题

canvas的drawImage使用跨域图片时候,会报错,解决方法如下: 1. 使用base64替换跨域图片 如果图片不大,且只有几张,可以使用base64,来代替跨域引用图片. 2. 设置image的crossOrigin属性,并且设置服务端的 Access-Control-Allow-Origin:* (或允许的域名) var cvs = document.getElementById('canvas'); var ctx = example.getContext('2d'); var im